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