R如何根据另一个变量添加行,然后从宽格式转换为长格式

发布于 2025-01-30 15:55:47 字数 1346 浏览 4 评论 0原文

我想分两个步骤重组我的数据框架。

原始数据集:

structure(list(ID = c("1", "2", "3", "4", "5", "6"), Type = c("A", 
"B", "B", "A", "A", "C"), A_Var1 = c("Yes", "None", "None", "Yes", 
"None", "None"), A_Var2 = c("Yes", "None", "None", "Yes", "Yes", 
"None"), A_Var3 = c("Yes", "None", "None", "Yes", "Yes", "Yes"
), B_Var1 = c("NA", "None", "None", "NA", "NA", "None"), B_Var2 = c("NA", 
"None", "None", "NA", "NA", "Yes"), B_Var3 = c("NA", "None", 
"None", "NA", "NA", "Yes")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

首先,我希望根据 type 的顺序顺序为每个 id 添加新行。数据框架中有3种类型,并且顺序为a,b,c。对于每个 id ,如果我看到类型B,我想添加一个使用相同 id 但上面键入A的行;如果我看到了C型,那么我将添加两个具有相同ID的行,但在上方键入A和类型B。

其次,我希望在步骤1之后将数据框架从宽格式转换为长格式。因此,我将 var1 var2 var3 填充到相应的类型中。

其他数据集:

structure(list(ID = c("1", "2", "2", "3", "3", "4", "5", "6", 
"6", "6"), Type = c("A", "A", "B", "A", "B", "A", "A", "A", "B", 
"C"), Var1 = c("Yes", "None", "None", "None", "None", "Yes", 
"None", "None", "None", NA), Var2 = c("Yes", "None", "None", 
"None", "None", "Yes", "Yes", "None", "Yes", NA), Var3 = c("Yes", 
"None", "None", "None", "None", "Yes", "Yes", "Yes", "Yes", NA
)), row.names = c(NA, -10L), class = "data.frame")

我很难弄清楚如何以一个不错的顺序完成这两个步骤。感谢任何评论!

I would like to restructure my data frame in two steps.

Original data set:

structure(list(ID = c("1", "2", "3", "4", "5", "6"), Type = c("A", 
"B", "B", "A", "A", "C"), A_Var1 = c("Yes", "None", "None", "Yes", 
"None", "None"), A_Var2 = c("Yes", "None", "None", "Yes", "Yes", 
"None"), A_Var3 = c("Yes", "None", "None", "Yes", "Yes", "Yes"
), B_Var1 = c("NA", "None", "None", "NA", "NA", "None"), B_Var2 = c("NA", 
"None", "None", "NA", "NA", "Yes"), B_Var3 = c("NA", "None", 
"None", "NA", "NA", "Yes")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

First, I am hoping to add new rows for each ID based on the sequential order of Type. There are 3 types in the data frame, and the order is A,B,C. For each ID, if I see type B, I would like to add a row with the same ID but type A above; if I see type C, then I will add two rows with same ID but type A and type B above.

Second, I am hoping to transform the data frame after step 1 from a wide format to a long format. So I fill Var1, Var2, Var3 to its corresponding type.

Excepted data set:

structure(list(ID = c("1", "2", "2", "3", "3", "4", "5", "6", 
"6", "6"), Type = c("A", "A", "B", "A", "B", "A", "A", "A", "B", 
"C"), Var1 = c("Yes", "None", "None", "None", "None", "Yes", 
"None", "None", "None", NA), Var2 = c("Yes", "None", "None", 
"None", "None", "Yes", "Yes", "None", "Yes", NA), Var3 = c("Yes", 
"None", "None", "None", "None", "Yes", "Yes", "Yes", "Yes", NA
)), row.names = c(NA, -10L), class = "data.frame")

I have a hard time figuring out how to accomplish both steps in a nice order. Would appreciate any comment!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烛影斜 2025-02-06 15:55:47

使用以下代码:

df1 %>% 
  mutate(across(everything(), na_if, 'NA')) %>%
  select(-Type) %>%
  pivot_longer(-ID, names_to = c('Type', '.value'),
               names_sep = '_', values_drop_na = TRUE)

# A tibble: 9 x 5
  ID    Type  Var1  Var2  Var3 
  <chr> <chr> <chr> <chr> <chr>
1 1     A     Yes   Yes   Yes  
2 2     A     None  None  None 
3 2     B     None  None  None 
4 3     A     None  None  None 
5 3     B     None  None  None 
6 4     A     Yes   Yes   Yes  
7 5     A     None  Yes   Yes  
8 6     A     None  None  Yes  
9 6     B     None  Yes   Yes  

Use the following code:

df1 %>% 
  mutate(across(everything(), na_if, 'NA')) %>%
  select(-Type) %>%
  pivot_longer(-ID, names_to = c('Type', '.value'),
               names_sep = '_', values_drop_na = TRUE)

# A tibble: 9 x 5
  ID    Type  Var1  Var2  Var3 
  <chr> <chr> <chr> <chr> <chr>
1 1     A     Yes   Yes   Yes  
2 2     A     None  None  None 
3 2     B     None  None  None 
4 3     A     None  None  None 
5 3     B     None  None  None 
6 4     A     Yes   Yes   Yes  
7 5     A     None  Yes   Yes  
8 6     A     None  None  Yes  
9 6     B     None  Yes   Yes  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文