如何在数据范围列表中函数case_当case_?
我有一个dataFrame的列表,希望根据case_时更改值。我不知道如何申请列表。
mutate(FLOOR = case_when("FLOOR" == 1 ~ "1",
TRUE ~ "0"),
LIGHTING = case_when(LIGHTING == 1 ~ "1",
TRUE ~ "0"),
COOKING = case_when(COOKING == 1 ~ "1",
TRUE ~ "0"),
DRINKING_W = case_when(DRINKING_W == 1 ~ "1",
TRUE ~ "0"),
TOILET = case_when(TOILET == 1 ~ "1",
TRUE ~ "0")
这是一个可重现的dataFrame示例。我可以在单个数据框架上申请,但可以在列表上申请。
df1 <- dput(head(aceh[[2]]))
structure(list(FLOOR = c(1, 3, 3, 3, 3, 3), LIGHTING = c(1, 1,
1, 1, 4, 1), COOKING = c(3, 5, 5, 5, 5, 3), DRINKING_W = c(1,
2, 2, 5, 7, 8), TOILET = c(1, 1, 1, 1, 1, 4), SEPTIC_TAN = c(1,
2, 2, 3, 1, 0), TELEPHONE = c(2, 4, 4, 4, 4, 2), TENURE = c(1,
1, 1, 4, 1, 1)), row.names = c(7L, 9L, 10L, 65L, 66L, 10578L), class = "data.frame")
df2 <- structure(list(FLOOR = c(3, 3, 3, 3, 6, 3), LIGHTING = c(1, 1,
1, 1, 2, 1), COOKING = c(5, 5, 5, 5, 5, 5), DRINKING_W = c(7,
7, 7, 7, 8, 8), TOILET = c(3, 3, 3, 3, 4, 4), SEPTIC_TAN = c(3,
3, 3, 3, 0, 0), TELEPHONE = c(2, 2, 2, 2, 4, 2), TENURE = c(1,
1, 1, 1, 2, 2)), row.names = 252098:252103, class = "data.frame")
sample <- as.list(df1, df2)
谢谢您的帮助。
I have do a list of dataframe want to change the values according to case_when function. I don't know how to apply on a list.
mutate(FLOOR = case_when("FLOOR" == 1 ~ "1",
TRUE ~ "0"),
LIGHTING = case_when(LIGHTING == 1 ~ "1",
TRUE ~ "0"),
COOKING = case_when(COOKING == 1 ~ "1",
TRUE ~ "0"),
DRINKING_W = case_when(DRINKING_W == 1 ~ "1",
TRUE ~ "0"),
TOILET = case_when(TOILET == 1 ~ "1",
TRUE ~ "0")
This is one dataframe reproducible example. I can apply on a single dataframe but neet to apply on a list.
df1 <- dput(head(aceh[[2]]))
structure(list(FLOOR = c(1, 3, 3, 3, 3, 3), LIGHTING = c(1, 1,
1, 1, 4, 1), COOKING = c(3, 5, 5, 5, 5, 3), DRINKING_W = c(1,
2, 2, 5, 7, 8), TOILET = c(1, 1, 1, 1, 1, 4), SEPTIC_TAN = c(1,
2, 2, 3, 1, 0), TELEPHONE = c(2, 4, 4, 4, 4, 2), TENURE = c(1,
1, 1, 4, 1, 1)), row.names = c(7L, 9L, 10L, 65L, 66L, 10578L), class = "data.frame")
df2 <- structure(list(FLOOR = c(3, 3, 3, 3, 6, 3), LIGHTING = c(1, 1,
1, 1, 2, 1), COOKING = c(5, 5, 5, 5, 5, 5), DRINKING_W = c(7,
7, 7, 7, 8, 8), TOILET = c(3, 3, 3, 3, 4, 4), SEPTIC_TAN = c(3,
3, 3, 3, 0, 0), TELEPHONE = c(2, 2, 2, 2, 4, 2), TENURE = c(1,
1, 1, 1, 2, 2)), row.names = 252098:252103, class = "data.frame")
sample <- as.list(df1, df2)
Thank you for your helping.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的示例仍然不起作用,即我假设您想要
list(DF1,DF2)
,而不是as.list(df1,df2)
,但这是一个简单的解决方案。基本上,只需使用映射(如果需要基本R解决方案(如果需要),它会贯穿每个列表元素并应用.f
中指定的函数。在您的情况下,FCUNTION只是为了进行一些重新编码:Your example still doesn't work, i.e. I assume you want
list (df1, df2)
, notas.list(df1, df2)
, but here's a simple solution. Basically just use map (or lapply if you want a base R solution) which goes through each list element and applies the function specified in.f
. And in your case the fcuntion is simply to do some recoding: