Pregunta

Here is my colnames for my data frame.

colnames(new)[3:ncol(new)]
[1] "tag12" "tag13" "tag14" "tag15" "tag16" "tag17" "tag18" "tag22" "tag23"
[10] "tag24"

How do I create something like

new.1<- tag12 + tag13 + tag14......

Here is my first try.

for ( i in 3:ncol(all.1))
new.1<-as.data.frame(paste(colnames(all.1)[c(i-1)],colnames(all.1)[c(i)],sep="+"))

      paste(colnames(all.1)[c(i - 1)], colnames(all.1)[c(i)], sep = "+")
1                                                              tag3+tag4
¿Fue útil?

Solución

Why not just:

new.1 <- paste0( colnames(new)[3:ncol(new)], collapse="+")

If you give just one vector to paste or paste0 you get a vector back with as many items as in the original vector. If you want to concatenate the items with "+" signs between them you need to us collapse.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top