2014-02-19 148 views
1

的另一个字符串我想将字符串“19”粘贴到另一个字符串中,但确定了不同的顺序位置。例如,在下面我需要粘贴从第二个数字向后计数的“19”,在列b中给出一个值:“1101.77”到“1101.1977”。我怎样才能做到这一点?将字符串粘贴到位置为

df<- structure(list(a = c("1101.77", "10990", "103250", "10.96", "9.906", "10.70", 
"11.43", "11.41", "10.48512", "11.19"), b = c(1, 3, 2, 0, 0, 0, 1, 2, 
4, 0)), .Names = c("a", "b"), row.names = c(NA, -10L), class = "data.frame") 

我已经试过这样的,但是,“19”总是在字符串的结尾粘贴不管我怎么变开始和结束参数:

rows <- which(df$b==1) 
if(length(rows)>0){ 
df$a[rows] <- paste(str_sub(df$a[rows],start = 1, end = 6),'19',sep="") 
} 
+0

开始和结束都是字符串,按照你的方式,使用strsub(前四)和19 strsub(过去两年)并粘贴 – Ananta

+1

你可以粘贴'df $ a' b/c的期望输出我没有关注你之后的内容。 –

回答

1

尝试是这样的:

rows <- which(df$b==1) 
if(length(rows)>0){ 
print(df$a[rows]) 
df$a[rows] <- paste(str_sub(df$a[rows],start=-7,end=-3),'19',sep="",str_sub(df$a[rows],start = -2, end = -1)) 
print(df$a[rows]) 
} 
-1

的逻辑转换你的对象字符串,然后找到字符“。”位置,之后您可以插入新的字符串temp与您的值,然后再次将您的对象转换为十进制。 这个代码:

intori = 1101.77; 
strori = cstr(intOri); 
intPosPoint = indexof('.'); 
strtmp = left(strori,intPosPoint-1); 
strtmp = strtmp &".19" & right(strori,2); 
decVal = cdec(strtmp); 
+0

它看起来不像是R的答案... – landroni

相关问题