r - Lapply to Add Columns to Each Dataframe in a List -
this question has answer here:
my question two-fold..
- i have list of dataframes, , using lapply in r, add column each dataframe in list.
- the added column should take values sequentially list, if possible. have list same length list of dataframes, , each value in list should added column value.
the reason i'm doing because file name of each data set i'm importing has date info, e.g. file name contains jun12_2003. want import each data set, , assign column year , date, taking info file name (so far doing part regexp).
thanks help!
use map
. short mapply(..., simplify = false)
suggested ari.
df1 <- data.frame(x = runif(3), y = runif(3)) df2 <- data.frame(x = runif(3), y = runif(3)) dfs <- list(df1, df2) years <- list(2013, 2014) map(cbind, dfs, year = years) # [[1]] # x y year # 1 0.8843945 0.6285246 2013 # 2 0.8400041 0.1369520 2013 # 3 0.4398870 0.4660476 2013 # # [[2]] # x y year # 1 0.4153315 0.5831114 2014 # 2 0.9685105 0.2398060 2014 # 3 0.9507591 0.7585670 2014
Comments
Post a Comment