How to create variables in a data frame based on existing variables in a for loop

  • A+
Category:Languages

I have a data frame that has a number of variables in it that I want to concatenate into new variables in that same data frame. A simplified version of my data frame df looks like this:

first.1 second.1 first.2 second.2  1222 3223 3333 1221  1111 2212 2232 2113  

Here is how I do it inefficiently without a for loop:

df$concatenated.1 <- paste0(df$first.1,"-",df$second.1) df$concatenated.2 <- paste0(df$first.2,"-",df$second.2) 

Which results in the following data frame df:

first.1 second.1 first.2 second.2 concatenated.1 concatenated.2  1222 3223 3333 1221 1222-3223 3333-1221  1111 2212 2232 2113 1111-2212 2232-2113  

I have a lot more than 2 pairs of variables to concatenate, so I would like to do this in a for loop:

for (i in 1:2){ ?? } 

Any ideas on how to accomplish this?

 


If your real data has names which follow a clear pattern as in this example data, Ronak's split / lapply answer is probably best. If not, you can just create vectors of the names and use Map with paste.

new.names <- paste0('concatenated.', 1:2) names.1 <- paste0('first.', 1:2) names.2 <- paste0('second.', 1:2)  df[new.names] <- Map(paste, df[names.1], df[names.2], sep = '-')  df  #   first.1 second.1 first.2 second.2 concatenated.1 concatenated.2 # 1    1222     3223    3333     1221      1222-3223      3333-1221 # 2    1111     2212    2232     2113      1111-2212      2232-2113 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: