这是我的样本:
stringa = c("a","b","c")
stringb = c("high","low","average")
index = c(1,2,3)
data <- data.frame(stringa,stringb,index)
我想连接stringa和stringb,并在另一列中附加相应的索引。例如,结果的第一行应该是&#34;高的&#34;索引&#34; 1&#34;。
现在我已经使用这个函数来连接两个字符串:
c(outer(a, b, paste))
答案 0 :(得分:1)
对于&#34;高&#34;,&#34; b低&#34;,&#34; c平均值&#34;你可以这样做:
stringa = c("a","b","c")
stringb = c("high","low","average")
index = c(1,2,3)
data.frame(concatenated = paste(stringa, stringb),index)
concatenated
1 a high
2 b low
3 c average
对于stringa和stringb的完整排列:
stringa = c("a","b","c")
stringb = c("high","low","average")
data.frame(concatenated = c(outer(stringa, stringb, paste) ) )
concatenated
1 a high
2 b high
3 c high
...
9 c average
如果要显式添加行索引:
df = data.frame(concatenated = c(outer(stringa, stringb, paste) ) )
df$index = rownames(df)
df
concatenated index
1 a high 1
2 b high 2
3 c high 3
...
9 c average 9