我有一個數據框與幾個變量。我要的是創建一個使用(串聯)變量名的字符串,但在他們之間的其他什麼東西......連接字符串
下面是一個簡單的例子(一些變量縮小到只有3,而我其實有很多 )
彌補一些數據幀
df1 <- data.frame(1,2,3) # A one row data frame
names(df1) <- c('Location1','Location2','Location3')
實際代碼...
len1 <- ncol(df1)
string1 <- 'The locations that we are considering are'
for(i in 1:(len1-1)) string1 <- c(string1,paste(names(df1[i]),sep=','))
string1 <- c(string1,'and',paste(names(df1[len1]),'.'))
string1
這給了...
[1] "The locations that we are considering are"
[2] "Location1"
[3] "Location2"
[4] "Location3 ."
但我想
我們正在考慮的地點是LOCATION1,LOCATION2和LOCATION3。
我肯定是有一些的,你會知道很多更簡單的方法... 謝謝你的時間......
對不起@ cbeleites我做了一些編輯你的答案 – 2012-03-22 15:41:30
在這種情況下,'後貼( 「考慮」,粘貼(頭(地名(DF),-1) ,collapse =「,」),「and」,tail(names(df),1),「。」,sep =「」)。 – cbeleites 2012-03-22 15:48:49