在“整洁"之后重新组织具有多种标头类型的数据帧R中的方法 [英] Reorganizing dataframe with multiple header types following "tidy" approach in R
问题描述
我有一个看起来像这样的数据框:
I have a dataframe that looks like somewhat like this:
Age A1U_sweet A2F_dip A3U_bbq C1U_sweet C2F_dip C3U_bbq Comments
23 1 2 1 NA NA NA Good
54 NA NA NA 4 1 2 ABCD
43 2 4 7 NA NA NA HiHi
我正在尝试以如下所示的方式重新组织它,使其更加整洁".有没有一种方法可以让我做到这一点,它也以与下面其他变量所示相同的样式合并了 Age 和 Comments 列?您建议如何合并它们 - 下面显示了一个想法,但我愿意接受其他建议.我将如何修改以下代码以考虑多种不同样式的列名?
I am trying to reorganize it in way shown below to make it more "tidy". Is there a way for me to do this that also incorporates the Age and Comments columns in the same style as shown for the other variables below? How would you suggest incorporating them - one idea is shown below, but I am open to other suggestions. How would I modify the following code in order to account for multiple different styles of column name?
library(tidyr)
df <- data.frame(id = 1:nrow(df), df)
dfl <- gather(df, key = "key", value = "value", -id)
dfl <- separate(dfl, key, into = c("key", "kind", "type"), sep = c(1, 4))
df2 <- spread(dfl, key, value)
df2
## id kind type A C
## 1 1 Age Age 23 23
## 2 1 1U_ sweet 1 NA
## 3 1 2F_ dip 2 NA
## 4 1 3U_ bbq 1 NA
## 5 1 Com Com Good Good
## 6 2 Age Age 54 54
## 7 2 1U_ sweet NA 4
## 8 2 2F_ dip NA 1
## 9 2 3U_ bbq NA 2
##10 2 Com Com ABCD ABCD
##11 3 Age Age 43 43
##12 3 1U_ sweet 2 NA
##13 3 2F_ dip 4 NA
##14 3 3U_ bbq 7 NA
##15 3 Com Com HiHi HiHi
我将如何修改以下代码以将数据恢复到原来的状态?
And how would I modify the following code to return the data back to how it originally was?
df <- gather(df2, key = "key", value = "value", A, B, C)
df <- unite(df, "key", key, kind, type, sep = "")
df <- spread(df, key, value)
对于上下文,这个问题是由 Ista 在这个问题下的评论提示的:根据匹配的列标题名称开头组合 R 中的列
For context, this question was prompted by Ista's comment under this question: Combining columns in R based on matching beginnings of column title names
推荐答案
因为 Age
和 Comments
大概是在原始数据中的任何一行的级别上衡量的,带上他们去兜风:
Since Age
and Comments
are presumably measured at the level of whatever a row in your original data is, just bring them along for the ride:
df <- data.frame(id = 1:nrow(df), df)
dfl <- gather(df, key = "key", value = "value", -id, -Age, -Comments)
dfl <- separate(dfl, key, into = c("key", "kind", "type"), sep = c(1, 4))
df2 <- spread(dfl, key, value)
df2
df2 <- transform(df2, B = ifelse(is.na(A), C, A))
df2
df <- gather(df2, key = "key", value = "value", A, B, C)
df <- unite(df, "key", key, kind, type, sep = "")
df <- spread(df, key, value)
df
这篇关于在“整洁"之后重新组织具有多种标头类型的数据帧R中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!