我有一个大的 Dataframe 列表,它们有相同的列名。我想写的代码应用相同的数据争吵规则到每个包含在列表中的 Dataframe 。从R全局环境A list输出列表的示例为了提供列表中包含的 Dataframe 的示例,我在这里创建了两个具有可运行代码的示例 Dataframe :
df<-data.frame(Event.Type = c("SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER6", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER6", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER6","SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER5","SEVT_TYPE3_AROUSAL", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER1", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER6", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_USER6", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5","SEVT_USER6", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_USER5", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER4", "SEVT_TYPE3_AROUSAL", "SEVT_USER4", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_USER1", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_TYPE3_AROUSAL", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER1", "SEVT_USER5", "SEVT_TYPE3_AROUSAL", "SEVT_USER1", "SEVT_USER6", "SEVT_TYPE3_AROUSAL", "SEVT_TYPE3_AROUSAL", "SEVT_USER5", "SEVT_USER5", "SEVT_USER5", "SEVT_USER3", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5","SEVT_USER5","SEVT_USER5"),
(Event.Label = c(NA, "Auto Trigger", "AASM", "AASM", "AASM", "Double Trigger", "AASM", "AASM", "AASM", "Auto Trigger", "Double Trigger", "AASM", "AASM", "Double Trigger", "AASM", "AASM", "AASM", "AASM", "AASM", NA, "AASM", "AASM", "AASM", "AASM", "AASM", "AASM", NA, "AASM", "AASM", "AASM", "AASM", "Auto Trigger", "Double Trigger", "AASM", "Auto Trigger", "Double Trigger","AASM", "Double Trigger", "AASM", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "AASM", "Double Trigger", NA, "AASM", "AASM", "AASM", "AASM", NA, "Double Trigger", "AASM", "Double Trigger", NA, "Double Trigger", NA, "AASM", "Double Trigger", "AASM", "High Leak", "AASM", "AASM", "Auto Trigger", "AASM", "Double Trigger", "Auto Trigger", "Double Trigger", "Double Trigger", "AASM", "AASM", "AASM", "AASM", "AASM", "AASM", "AASM", "AASM", "Double Trigger", "AASM", "AASM", "AASM", NA, "AASM", "Auto Trigger", "Double Trigger", "AASM", "AASM", "AASM", NA, "AASM", "AASM", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", "Auto Trigger", NA, "Double Trigger", "AASM", "AASM", "AASM", "AASM", "Double Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", NA, "Double Trigger", "Double Trigger", "Double Trigger", "AASM", "AASM", "Double Trigger", "AASM", "Ineffective Effort", "AASM", "Ineffective Effort", "AASM", "AASM", "AASM", "Double Trigger", "AASM", "Double Trigger", "High Leak", "Double Trigger", NA, "AASM", "Auto Trigger", NA, "Double Trigger", "AASM", "High Leak", "Double Trigger", "AASM", "High Leak", "Auto Trigger", "AASM", "AASM", "Double Trigger", "Double Trigger", "Double Trigger", "Hyperventilation", "Double Trigger", "Auto Trigger","Double Trigger", "Double Trigger", "Double Trigger", "Double Trigger")))
df2<- data.frame(Event.Type = c("SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER4", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_USER6", "SEVT_USER6", "SEVT_USER6", "SEVT_USER5", "SEVT_USER1", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER4", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER4", "SEVT_USER5", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER4", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER5", "SEVT_USER5", "SEVT_USER4", "SEVT_USER6", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER1", "SEVT_USER4", "SEVT_USER6", "SEVT_USER5", "SEVT_USER1", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER6", "SEVT_USER5", "SEVT_USER4", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_SPO2_DESAT", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER5", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_USER4", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER6", "SEVT_USER6", "SEVT_USER5", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_USER6", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_SPO2_DESAT", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER2", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER5", "SEVT_USER2", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER6", "SEVT_SPO2_DESAT", "SEVT_USER2", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_SPO2_DESAT", "SEVT_USER6", "SEVT_USER2", "SEVT_USER5", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER6", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER2", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_OBSTRUCTIVE_APNEA", "SEVT_USER5", "SEVT_USER4", "SEVT_USER6", "SEVT_USER2", "SEVT_USER5", "SEVT_OBSTRUCTIVE_HYPOPNEA", "SEVT_USER2", "SEVT_USER5", "SEVT_USER6", "SEVT_USER2", "SEVT_USER2", "SEVT_USER5","SEVT_USER4", "SEVT_USER2", "SEVT_USER5", "SEVT_USER2", "SEVT_USER2", "SEVT_USER5", "SEVT_USER2", "SEVT_USER6", "SEVT_USER5"),
Event.Label = c("Auto Trigger", "Double Trigger", "Double Trigger", NA, NA, "Ineffective Effort", NA, "Auto Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", "Auto Trigger", "Auto Trigger", "Auto Trigger", "Double Trigger", "High Leak", "Auto Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", NA, NA, "Auto Trigger", "Double Trigger", NA, NA, "Auto Trigger", "Double Trigger", NA, "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", NA, NA, "Auto Trigger", "Double Trigger", "Double Trigger", NA, NA, "Auto Trigger", "Double Trigger", "Double Trigger", NA, "Double Trigger", NA, "Auto Trigger", NA, "Auto Trigger", "Ineffective Effort", "Auto Trigger", NA, "Double Trigger", "Double Trigger", NA, NA, "Auto Trigger", NA, "Auto Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", NA, "Auto Trigger", "Double Trigger", NA, "Auto Trigger", "Ineffective Effort", "Double Trigger", "Auto Trigger", NA, "Auto Trigger", "Double Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", NA, "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", NA, "Auto Trigger", "Auto Trigger", "Double Trigger", "Double Trigger", NA, NA, "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Auto Trigger", "Double Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Ineffective Effort", NA, NA, "Double Trigger", "Double Trigger", "Ineffective Effort", "Auto Trigger", NA, "High Leak", "Ineffective Effort", "Auto Trigger", "Double Trigger", "High Leak", "Double Trigger", NA, NA, "Auto Trigger", "Auto Trigger", "Double Trigger", "Ineffective Effort", "Double Trigger", "Double Trigger", NA, NA, "Double Trigger", NA, "Auto Trigger", NA, "Double Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", "Ineffective Effort", "Auto Trigger", "Double Trigger", "Auto Trigger", "Double Trigger", NA, "Auto Trigger", "Auto Trigger", "Double Trigger", "Double Trigger", NA, NA, "Auto Trigger", "Double Trigger", "Auto Trigger", NA, NA, "Auto Trigger", "Double Trigger", NA, NA, NA, "Hypoventilation", NA, "Auto Trigger", "Double Trigger", "Hypoventilation", "Double Trigger", NA, "Auto Trigger", NA, "Hypoventilation", "Double Trigger", NA, NA, "Auto Trigger", "Hypoventilation", "Double Trigger", NA, "Auto Trigger", NA, "Hypoventilation", NA, NA, "Double Trigger", "Ineffective Effort", "Auto Trigger", "Hypoventilation", "Double Trigger", NA, "Hypoventilation", "Double Trigger", "Auto Trigger", "Hypoventilation", "Hypoventilation", "Double Trigger", "Ineffective Effort", "Hypoventilation", "Double Trigger", "Hypoventilation", "Hypoventilation", "Double Trigger", "Hypoventilation", "Auto Trigger", "Double Trigger" ))
A_list<- list(df, df2)
字符串
针对单个 Dataframe (df 2)的预期数据争吵的示例:
df3<- df2 %>% mutate(Event.Type =
case_when(Event.Type == "SEVT_OBSTRUCTIVE_HYPOPNEA" ~ "hypopnea",
Event.Type == "SEVT_OBSTRUCTIVE_APNEA" ~ "obstruct_apnea",
Event.Type == "SEVT_MIXED_APNEA" ~ "mixed_apnea",
Event.Type == "SEVT_CENTRAL_APNEA" ~ "central_apnea")) %>%
mutate(Event.Label=
case_when(Event.Label == "Auto Trigger" ~ "auto_trig",
Event.Label == "Double Trigger" ~ "double_trig",
Event.Label == "Ineffective Effort" ~ "ineffective_eff",
Event.Label == "High Leak" ~ "high_leak",
Event.Label == "Hyperventilation" ~ "hyperventilation",
Event.Label == "Hypoventilation" ~ "hypoventilation")) %>%
mutate(Events = coalesce(Event.Type, Event.Label))
df4<- df3[!(is.na(df3$Events)), ]
型
阅读了在线资源,我认为编写包含purr:map的代码是最有效的方法;但是,我似乎找不到任何使用dplyr::mutate或dplyr::case_when函数示例。我认为这是处理手头任务的有效方法,对吗?如果是这样的话,任何关于如何编写代码以将数据争吵应用于大量 Dataframe 列表的输入都是非常感谢的!
2条答案
按热度按时间e5nszbig1#
看起来你想要这样的东西:
字符串
或者按照Ric的建议去做:
型
在map中,你可以像上面的例子一样传递一个函数,或者作为一个匿名函数传递(如第一个例子)。在R中有几种不同的格式化匿名函数的方法,我使用的只是其中一种。
map
函数的主要替代方案是apply
函数(sapply
,lapply
等)。hwamh0ep2#
我们也可以使用
dplyr::rowwise()
而不是purrr::map()
。对于这种操作,最好将数据放在嵌套data.frame
中。下面我用dplyr::nest_by()
创建这个嵌套的data.frame
。请注意,这会自动应用rowwise()
,因此无需直接使用它。字符串
数据来自OP
型
由reprex package(v2.0.1)于2023-07-24创建