简体   繁体   English

循环遍历数据框以生成新列

[英]Loop through data frames to produce new column

I have 10 data frames (Shape1,Shape 2,Shape 3 etc.), each containing the same the column names.我有 10 个数据框(Shape1、Shape 2、Shape 3 等),每个数据框都包含相同的列名。 How do i for loop this command through the 10 data frames so each contains a new column called integration?我如何在 10 个数据帧中循环此命令,以便每个数据帧都包含一个称为集成的新列?

Shape1$Integration<-cumtrapz(Shape1$Time, Shape1$CA) 

many thanks非常感谢

In base R, you can create a list of dataframe using mget , loop over each and create a new column with transform .在基础 R 中,您可以使用mget创建 dataframe 列表,遍历每个列表并使用transform创建一个新列。

lapply(mget(paste0("Shape", 1:10)), function(x) 
        transform(x, Integration = cumtrapz(Time, CA)))

In tidyverse that can be done astidyverse中,可以这样做

library(tidyverse)
map(mget(paste0("Shape", 1:10)), ~.x %>% mutate(Integration = cumtrapz(Time, CA)))

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

相关问题 如何遍历数据框列表并将名称的数字部分分配为数据框的新列值? - How to loop through a list of data frames and assign the numeric portion of the name as a new column value for the data frame? 使用逻辑从现有的多列数据帧生成新的多列数据帧 - Produce new multi-column data frame from existing multi-column data frames with logic 每次迭代都调用新的数据框:遍历数据框? - Call new data frame for each iteration: Loop through data frames? for循环浏览数据帧列表并将结果保存在新数据帧中 - for loop through a list of data frames and save results in new data frame For循环遍历数据框中的模式匹配列并生成ggplot - For loop to iterate through pattern matched column in data frame and produce ggplot 通过数据帧循环子集 - loop subset through data frames 遍历数据帧列表 - Loop through list of data frames 循环创建新的数据帧 - Create new data frames in a loop 尝试生成一个函数,该函数根据列中的值分隔数据框,然后从该分隔数据生成新的数据框 - Trying to produce a function that separates a data frame, based on values within a column, and then produces new data frames from that separated data 如何遍历数据框列表以在 R 中设置列​​名? - How to loop through through list of data frames to set column names in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM