[英]How to make a function that creates a column with combined observations
I am obviously new to data cleaning and I am having trouble cleaning a survey export.我显然是数据清理的新手,并且在清理调查导出时遇到了麻烦。 This is how my data frame looks in raw form.这就是我的数据框以原始形式显示的样子。
Var1 Colname1 Colname2 Colname3 Var2
Observation1 NA NA Val1 Val_1
Observation2 NA Val2 NA Val_1
Observation3 Val3 NA NA Val_1
Observation4 Val4 Val5 NA Val_2
Observation5 NA NA Val6 Val_2
I would like to have my data cleaned to look like this:我想将我的数据清理成这样:
Var1 SubVar1 Var2
Observation1 Val1 Val_1
Observation2 Val2 Val_1
Observation3 Val3 Val_1
Observation4 Val4 Val_2
Observation4 Val5 Val_2
Observation5 Val6 Val_2
I have tried to remove NA values:我试图删除 NA 值:
df1 <- na.omit(c(Colname1, Colname2, Colname3))
The problem is that it will delete all rows because there is an NA in every row.问题是它会删除所有行,因为每一行都有一个 NA。 I have also tried to concatenate the values and then use the separate_rows() function, but that will only work with observations that only have one value in one column.我还尝试连接值,然后使用 separate_rows() 函数,但这仅适用于在一列中只有一个值的观察。 For observations that contain values in multiple columns (see Observation4), this will not work.对于在多列中包含值的观察(请参阅 Observation4),这将不起作用。
Thanks for any help you guys can provide!感谢你们提供的任何帮助!
Try,尝试,
data %>% mutate(SubVar1 = coalesce(Colname1,Colname2,Colname3)) %>%
select(Var1, SubVar1, Var2)
I would think of this as a pivot (reshaping) operation from wide to long:我会认为这是从宽到长的枢轴(重塑)操作:
library(dplyr)
library(tidyr)
data %>%
pivot_longer(cols = Colname1:Colname3, values_to = "SubVar1") %>%
filter(!is.na(SubVar1)) %>%
select(Var1, SubVar1, Var2)
# # A tibble: 6 × 3
# Var1 SubVar1 Var2
# <chr> <chr> <chr>
# 1 Observation1 Val1 Val_1
# 2 Observation2 Val2 Val_1
# 3 Observation3 Val3 Val_1
# 4 Observation4 Val4 Val_2
# 5 Observation4 Val5 Val_2
# 6 Observation5 Val6 Val_2
To understand what's happening, run the first line, then the first and second line, then the first, second and third line, etc. See ?pivot_longer
for several other options in specifying which columns to pivot - you could name the explicitly, use a name pattern like names_pattern = "Colname"
or use the Colname1:Colname3
to select consecutive columns as I did above.要了解发生了什么,请运行第一行,然后是第一行和第二行,然后是第一行、第二行和第三行,等等。有关指定要透视的列的其他几个选项,请参阅?pivot_longer
- 您可以明确命名,使用 a名称模式,如names_pattern = "Colname"
或使用Colname1:Colname3
来选择连续的列,就像我上面所做的那样。
We can use base R
in a vectorized way with row/column indexing.我们可以通过行/列索引以矢量化方式使用base R
Subset the columns where the column names are 'Colname', then get the column index of non-NA element for each row with max.col
, cbind
the row sequence, extract the corresponding element and create the new data.frame
将列名为'Colname'的列子集,然后用max.col
获取每行非NA元素的列索引, cbind
行序列,提取对应的元素,创建新的data.frame
i1 <- startsWith(names(df1), "Colname")
data.frame(df1['Var1'], SubVar1 = df1[i1][cbind(seq_len(nrow(df1)),
max.col(!is.na(df1[i1]), "first"))], df1['Var2'])
Var1 SubVar1 Var2
1 Observation1 Val1 Val_1
2 Observation2 Val2 Val_1
3 Observation3 Val3 Val_1
4 Observation4 Val4 Val_2
5 Observation5 Val6 Val_2
df1 <- structure(list(Var1 = c("Observation1", "Observation2", "Observation3",
"Observation4", "Observation5"), Colname1 = c(NA, NA, "Val3",
"Val4", NA), Colname2 = c(NA, "Val2", NA, "Val5", NA), Colname3 = c("Val1",
NA, NA, NA, "Val6"), Var2 = c("Val_1", "Val_1", "Val_1", "Val_2",
"Val_2")), class = "data.frame", row.names = c(NA, -5L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.