I want to select the same 3 variables from dflist 1-4. dflist is the list containing 4 datasets I have already loaded. i wrote a code to achieve it. My code like this:
dfmul<-rbind(dflist[[1]][,c("siteid_public","childid_public","round")],
dflist[[2]][,c("siteid_public","childid_public","round")],
dflist[[3]][,c("siteid_public","childid_public","round")],
dflist[[4]][,c("siteid_public","childid_public","round")])
But this looks very chunky and not efficient. Can someone help to figure out how to I simplify it? Thanks a lot~~!
CodePudding user response:
You can loop over your list using lapply
, select your variables and bind all resulting rows.
dfmul <- do.call(rbind, lapply(dflist, "[", c("siteid_public",
"childid_public",
"round")))
CodePudding user response:
Another option:
library(tidyverse)
dfmul <- map_dfr(dflist, ~select(.x, siteid_public, childid_publicm, round))