r function/loop to add column and value to multiple dataframes

前端 未结 3 1175
终归单人心
终归单人心 2020-12-20 02:46

I have 8 data frames that I want to add a column called \'park\', then fill this column in w/ a value that comes from the last four characters of the data

3条回答
  •  一个人的身影
    2020-12-20 03:25

    I would put the data.frames in a named list and perform this task:

    rslt <- list(water_land_by_ownname_apis = water_land_by_ownname_apis, 
                 water_land_by_ownname_indu = water_land_by_ownname_indu)
    
    for (i in names(rslt)) {
      col <- unlist(strsplit(i, "_"))[5]
      rslt[[i]]$park <- col
    }
    
    do.call("rbind", rslt)
    

提交回复
热议问题