如何在数据范围列表中函数case_当case_?

发布于 2025-02-09 06:42:22 字数 1417 浏览 2 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2025-02-16 06:42:22

您的示例仍然不起作用,即我假设您想要list(DF1,DF2),而不是as.list(df1,df2),但这是一个简单的解决方案。基本上,只需使用映射(如果需要基本R解决方案(如果需要),它会贯穿每个列表元素并应用.f中指定的函数。在您的情况下,FCUNTION只是为了进行一些重新编码:

library(tidyverse)
list(df1, df2) |> 
  map(.f = ~ .x |> 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")))

[[1]]
      FLOOR LIGHTING COOKING DRINKING_W TOILET SEPTIC_TAN TELEPHONE TENURE
7         0        1       0          1      1          1         2      1
9         0        1       0          0      1          2         4      1
10        0        1       0          0      1          2         4      1
65        0        1       0          0      1          3         4      4
66        0        0       0          0      1          1         4      1
10578     0        1       0          0      0          0         2      1

[[2]]
       FLOOR LIGHTING COOKING DRINKING_W TOILET SEPTIC_TAN TELEPHONE TENURE
252098     0        1       0          0      0          3         2      1
252099     0        1       0          0      0          3         2      1
252100     0        1       0          0      0          3         2      1
252101     0        1       0          0      0          3         2      1
252102     0        0       0          0      0          0         4      2
252103     0        1       0          0      0          0         2      2

Your example still doesn't work, i.e. I assume you want list (df1, df2), not as.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:

library(tidyverse)
list(df1, df2) |> 
  map(.f = ~ .x |> 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")))

[[1]]
      FLOOR LIGHTING COOKING DRINKING_W TOILET SEPTIC_TAN TELEPHONE TENURE
7         0        1       0          1      1          1         2      1
9         0        1       0          0      1          2         4      1
10        0        1       0          0      1          2         4      1
65        0        1       0          0      1          3         4      4
66        0        0       0          0      1          1         4      1
10578     0        1       0          0      0          0         2      1

[[2]]
       FLOOR LIGHTING COOKING DRINKING_W TOILET SEPTIC_TAN TELEPHONE TENURE
252098     0        1       0          0      0          3         2      1
252099     0        1       0          0      0          3         2      1
252100     0        1       0          0      0          3         2      1
252101     0        1       0          0      0          3         2      1
252102     0        0       0          0      0          0         4      2
252103     0        1       0          0      0          0         2      2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文