简体   繁体   English

R,数据框列表,lapply后,结果存储也作为数据框列表,如何让结果存储在源名称中

[英]R, a list of data frame, after lapply, the result store also as a list of data frame, how to let the results store in the origin name

R, a list of data frames, after lapply some command, the result store also as a list of data frames, but how can I let the results store in each single data frame and use the same name as origin. R,数据帧列表,在lapply一些命令后,结果存储也作为数据帧列表,但是我怎样才能让结果存储在每个单独的数据帧中并使用与原点相同的名称。

for example:例如:
I have 17 data frames, they are all data frames with 100 observations and 50 columns.我有 17 个数据框,它们都是具有 100 个观察值和 50 列的数据框。 I want to drop the 6th column, and store the results into the original name.我想删除第 6 列,并将结果存储到原始名称中。

The desired output:所需的 output:

before: datanew_V102 100 observations and 50 columns之前:datanew_V102 100 个观察值和 50 列

datanew_V103 100 observations and 50 columns datanew_V103 100 个观察值和 50 列

datanew_V105 100 observations and 50 columns datanew_V105 100 个观察值和 50 列

after: datanew_V102 100 observations and 49 columns之后:datanew_V102 100 次观察和 49 列

datanew_V103 100 observations and 49 columns datanew_V103 100 个观察值和 49 列

datanew_V105 100 observations and 49 columns datanew_V105 100 个观察值和 49 列

My code is like:我的代码是这样的:

datalist = list(datanewV_102,datanewV_103,datanewV_105,datanewV_107,datanewV_108,datanewV_112,

datanewV_114,datanewV_115,datanewV_118,datanewV_121,datanewV_122,datanewV_126,datanewV_128,

datanewV_183,datanewV_90,datanewV_95,datanewV_99)

remove_col6 <- function(df){
  df[-6]
}
output_list <- lapply(datalist, remove_col6)

It gives the output_list as a list of 17 data frames.它将 output_list 作为 17 个数据帧的列表。 But if I want to call the first data frame, I need to use output_list[1] .但是如果我想调用第一个数据框,我需要使用output_list[1] How should I assign the original names in datalist to the data frame in the output_list .我应该如何将datalist中的原始名称分配给output_list中的数据框。

So that, all data frame with 100 observations and 49 columns datanewV_102 = output_list[1]因此,所有具有 100 个观测值和 49 列的数据框 datanewV_102 = output_list[1]

datanewV_103 = output_list[2] datanewV_103 = output_list[2]

datanewV_105 = output_list[3] datanewV_105 = output_list[3]

....... …………

datanewV_114 = output_list[7] datanewV_114 = output_list[7]

........ ...........

datanewV_99 = output_list[17] datanewV_99 = output_list[17]

How should I write into a single line of code in order to have the above result?为了得到上述结果,我应该如何写入一行代码?

Thanks.谢谢。

If we need to assign the output back to the object, we can use list2env (it is not recommended though, because we can do all the transformations within the list itself)如果我们需要将 output 分配回 object,我们可以使用list2env (虽然不推荐,因为我们可以在list本身内进行所有转换)

names(output_list) <- ls(pattern = '^datanewV_\\d+$')

Or if the order is important或者如果订单很重要

names(output_list) <- c('datanewV_102','datanewV_103','datanewV_105','datanewV_107','datanewV_108','datanewV_112',

 'datanewV_114','datanewV_115','datanewV_118','datanewV_121','datanewV_122','datanewV_126','datanewV_128',

  'datanewV_183','datanewV_90','datanewV_95','datanewV_99')

and now use list2env现在使用list2env

list2env(output_list, .GlobaleEnv)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM