简体   繁体   English

如何遍历数据框列表并将名称的数字部分分配为数据框的新列值?

[英]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?

I have a list of data frames我有一个数据框列表

list.files("data frame - 12345 - App", "data frame - 34567 - App", "data frame - 65849")

Each data frame looks like this每个数据框看起来像这样

Col1|Col2 |Col3
Row1 Info1 NewInfo1
Row2 Info2 NewInfo2

I would like to add the number portion of the list name as the value for each row of each dataframe's new ID column.我想将列表名称的数字部分添加为每个数据框的新 ID 列的每一行的值。 All three dataframes should like this所有三个数据框都应该像这样

Col1|Col2 |Col3    |ID Name
Row1 Info1 NewInfo1 12345
Row2 Info2 NewInfo2 12345

Col1|Col2 |Col3    |ID Name
Row1 Info1 NewInfo1 34567
Row2 Info2 NewInfo2 34567

Col1|Col2 |Col3    |ID Name
Row1 Info1 NewInfo1 65849
Row2 Info2 NewInfo2 65849

assuming such a list of data.frames假设这样一个 data.frames 列表

list(App_12345 =data.frame(a=letters[1:3], b=1:3),
     App_12345 =data.frame(a=letters[1:3], b=1:3))
$App_12345
  a b
1 a 1
2 b 2
3 c 3

$App_12345
  a b
1 a 1
2 b 2
3 c 3

you can use您可以使用

library(tidyverse)
list(App_12345 =data.frame(a=letters[1:3], b=1:3),
     App_34567 =data.frame(a=letters[1:3], b=1:3)) %>% 
  bind_rows(.id = "d") %>% 
  split(.$d)
$App_12345
          d a b
1 App_12345 a 1
2 App_12345 b 2
3 App_12345 c 3

$App_34567
          d a b
4 App_34567 a 1
5 App_34567 b 2
6 App_34567 c 3

By edding a mutate before the split you can transform as appropriate通过在split之前添加一个mutate ,您可以根据需要进行转换

mutate(d =gsub("App_", "", d)) 

As an alternative you can use purrr 's map function like作为替代方案,您可以使用purrr的 map function 之类的

list(App_12345 =data.frame(a=letters[1:3], b=1:3),
     App_34567 =data.frame(a=letters[1:3], b=1:3)) %>% 
map2(., names(.), ~mutate(.x, d = unlist(str_extract_all(.y, "[0-9]+"))))

暂无
暂无

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

相关问题 循环遍历数据框并将值分配给新列中的值 - Loop through data frame and assign a value to a value in a new column for循环浏览数据帧列表并将结果保存在新数据帧中 - for loop through a list of data frames and save results in new data frame 想要通过在循环中子集一个数据帧并基于i值分配每个数据帧名称来在R中创建新的数据帧 - Want to create new data frames in R by subsetting a data frame in a loop and assign each data frame name based on i value 如何在R数据帧中的列之间循环并在每次迭代中使用列名创建新的数据帧? - How to loop through the columns in an R data frame and create a new data frame using the column name in each iteration? 循环遍历数据框以生成新列 - Loop through data frames to produce new column 如何在for循环中为数据帧分配名称? 在R中 - How to assign name to data frames in a for loop? In R 我在一个列表中聚合了多个数据框。 新列表中的每个 df 都有 2cols:“Group.1”和“x”。 我想为每个 x 列分配其数据框的名称 - I aggregated multiple data frames in a list. Each df in the new list has 2cols:“Group.1” & “x”. I want assign each x column the name of its data frame 每次迭代都调用新的数据框:遍历数据框? - Call new data frame for each iteration: Loop through data frames? 在几个数据框中创建新列,其值取决于数据框的名称 - Create new column in several data frames with value dependent on name of data frame 如何在R中为数据框列表中的每个元素创建新变量,其数据框的名称及其值等于元素的位置 - How to create in R new variable for each element in a list of data frames with the name of data frame and its value equal to position of the element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM