R: Assigning vectors from a list of vectors as a value of the column in a data frame from a list of data frames -


i have 2 lists of equal length: 1 list of data frames, list of vectors, such length of each vector coincides number of rows in respective data frame of first list. want assign vectors second list value of first column in each data frame. easier explain code bellow:

for (i in seq_along(data)){    data[[c(i, 1)]] = links[[i]]                          } 

, data list of data frames, , links list of vectors. while code works fine, , speedwise there no particular need avoid for loops, wonder whether there other way perform same action without for?

since dataand links have same lengths, , replacing one-for-one, map() and/or mapply() choice. using data other answer,

data <- list(data.frame(a = 1:3, b = 4:6), data.frame(a = 10:14, b = 15:19)) links <- list(7:9, 20:24) 

you can do

map("[<-", data, 1, value = links) # [[1]] #   b # 1 7 4 # 2 8 5 # 3 9 6 # # [[2]] #     b # 1 20 15 # 2 21 16 # 3 22 17 # 4 23 18 # 5 24 19 

although r gods know how safe is. safer use anonymous function.

map(function(x, y, z) { x[y] <- z; x }, data, 1, links) 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -