如何使用purrr::map with dataframe list来修改特定数据帧中的列值,而不更改列表中的其他数据帧?
我有许多数据帧的列表(如何使用purrr::map with dataframe list来修改特定数据帧中的列值,而不更改列表中的其他数据帧?,r,dplyr,purrr,R,Dplyr,Purrr,我有许多数据帧的列表(survey08,survey09, survey10等)称为df_list 每个数据框包含两列,分别命名为year和employed #创建3个具有相同列名的数据帧 survey08您可以使用purrr::map_at,它只修改由名称或位置给出的元素 df_list %>% map_at(c("survey08", "survey09"), ~ filter(.x, year %in% 2008:2009)) %>% map_a
survey08
,survey09
,
survey10
等)称为df_list
year
和employed
#创建3个具有相同列名的数据帧
survey08您可以使用purrr::map_at
,它只修改由名称或位置给出的元素
df_list %>%
map_at(c("survey08", "survey09"),
~ filter(.x, year %in% 2008:2009)) %>%
map_at(c("survey08", "survey09"),
~ .x %>% mutate_at(vars(employed),
~ recode_factor(.,`1` = "yes", `2` = "no")))
$`survey08`
year employed
1 2008 yes
2 2008 no
3 2008 no
4 2008 yes
5 2008 no
$survey09
year employed
1 2009 yes
2 2009 yes
3 2009 yes
4 2009 no
5 2009 yes
$survey10
year employed
1 2010 2
2 2010 1
3 2010 1
4 2010 1
5 2010 1
使用filter
将删除要保留的其他data.frames。如果
而不是映射
,则需要映射。然后,您可以使用.p
来识别要在其上执行映射功能的项目
df_list %>%
map_if(.,
.f = ~ .x %>% mutate_at(vars(employed), ~recode_factor(.,`1` = "yes", `2` = "no")),
.p = c(T,T,F))
或
如果您已经知道要执行操作的列表,为什么不只对这些列表进行子集并重新编码呢
library(tidyverse)
df_list[c("survey08", "survey09")] <- df_list[c("survey08", "survey09")] %>%
map(~ .x %>% mutate_at(vars(employed), ~recode_factor(.,`1` = "yes", `2` = "no")))
df_list
#$survey08
# year employed
#1 2008 yes
#2 2008 no
#3 2008 no
#4 2008 yes
#5 2008 no
#$survey09
# year employed
#1 2009 yes
#2 2009 yes
#3 2009 yes
#4 2009 no
#5 2009 yes
#$survey10
# year employed
#1 2010 2
#2 2010 1
#3 2010 1
#4 2010 1
#5 2010 1
库(tidyverse)
df_列表[c(“调查08”、“调查09”)]%
map(~.x%>%mutate_at(变量(已使用),~recode_因子(,'1`=“是”,“2`=“否”))
df_列表
#$survey08
#雇佣年份
#1 2008年是的
#2008年第2号
#2008年第3号
#4 2008是的
#2008年第5号
#$survey09
#雇佣年份
#1 2009是的
#2 2009是的
#3 2009是的
#2009年第4号
#5 2009是的
#$survey10
#雇佣年份
#1 2010 2
#2 2010 1
#3 2010 1
#4 2010 1
#5 2010 1
使用lappy
和用户定义函数的基本R解决方案,评估年份是否小于2010
df_list2 <- lapply(df_list, function(x){
if (unique(x$year) < 2010){
x$employed <- as.character(factor(x$employed, levels = c(1, 2), labels = c("yes", "no")))
}
return(x)
})
df_list2
# $survey08
# year employed
# 1 2008 yes
# 2 2008 no
# 3 2008 no
# 4 2008 yes
# 5 2008 no
#
# $survey09
# year employed
# 1 2009 yes
# 2 2009 yes
# 3 2009 yes
# 4 2009 no
# 5 2009 yes
#
# $survey10
# year employed
# 1 2010 2
# 2 2010 1
# 3 2010 1
# 4 2010 1
# 5 2010 1
df_list 2是否需要同时使用map_at()
语句?似乎只适用于:df\u list%>%map\u at(c(“survey08”、“survey09”)、~.x%>%mutate\u at(vars(employed)”、~recode\u factor(,1`=“yes”、2
=“no”)、@JohnnySullivan这是不必要的。我不确定过滤器的用途,所以保持原样。
df_list2 <- lapply(df_list, function(x){
if (unique(x$year) < 2010){
x$employed <- as.character(factor(x$employed, levels = c(1, 2), labels = c("yes", "no")))
}
return(x)
})
df_list2
# $survey08
# year employed
# 1 2008 yes
# 2 2008 no
# 3 2008 no
# 4 2008 yes
# 5 2008 no
#
# $survey09
# year employed
# 1 2009 yes
# 2 2009 yes
# 3 2009 yes
# 4 2009 no
# 5 2009 yes
#
# $survey10
# year employed
# 1 2010 2
# 2 2010 1
# 3 2010 1
# 4 2010 1
# 5 2010 1