【发布时间】:2014-12-05 22:40:02
【问题描述】:
对不起,如果这是一个太大的例子。它看起来确实更真实,但我很难想出一个可以更好地解释我的情况的例子。
我想要的是一个整洁的 data.frame,我可以在其中使用摘要(平均)和绘图中的医疗状况 (已编辑) 我需要回答什么我是否正在尝试正确完成此操作。我是否想要一个带有用逗号分隔的值的巨大字符串的行?我需要把它分成更多的列吗?
来自我们数据库供应商的报告(实际数据已更改)。 报告没有给出唯一键。在我的 data.frames 中,person.id 在某些中是唯一的,而另一些则是这样的,具有多行 person.id 和值。
person.id <- c("1017", "1018", "1018", "1018", "1018", "1018", "1018",
"1018", "1018", "1018", "1018", "1019", "1019", "1020",
"1020")
med.condition <- c(NA, "Allergic rhinitis", "Allergic rhinitis",
"Atopic Dermatitis", "Atopic Dermatitis",
"Developmental Speech",
"Developmental Speech",
"Eye Condition", "Eye Condition", "Speech Delay",
"Speech Delay", "Allergic Reaction", NA, "Eczema",
"Obese")
cond.type <- c("Assessment", "Assessment", NA, "Assessment", NA, "Assessment",
NA, "Assessment", NA, "Assessment", NA, "Assessment",
"Assessment", "Assessment", "Assessment")
df <- data.frame(person.id, med.condition, cond.type)
看起来像:
person.id med.condition cond.type
1 1017 NA Assessment
2 1018 Allergic rhinitis Assessment
3 1018 Allergic rhinitis NA
4 1018 Atopic Dermatitis Assessment
5 1018 Atopic Dermatitis NA
6 1018 Developmental Speech Assessment
7 1018 Developmental Speech NA
8 1018 Eye Condition Assessment
9 1018 Eye Condition NA
10 1018 Speech Delay Assessment
11 1018 Speech Delay NA
12 1019 Allergic Reaction Assessment
13 1019 NA Assessment
14 1020 Eczema Assessment
15 1020 Obese Assessment
我希望行等于一个 person.id
我想让它看起来像这样吗(只显示前 5 列):使用了 taplly,它在整洁时失败了
condition1 condition2 condition3 condition4 condition5
1017 NA NA NA NA NA
1018 Allergic rhinitis Atopic Dermatitis Allergic Reaction Developmental Speech Eye Condition
1019 NA NA NA NA NA
1020 Eczema Obese NA NA NA
如何使数据集整洁?
med.condtion
1017 NA
1018 "Allergic rhinitis", "Atopic Dermatitis", "Developmental Speech", "Eye Condition", "Speech Delay", "Allergic Reaction"
1019 NA
1020 "Eczema" "Obese"
或者我需要换个新思路吗?
我累了轻拍,重塑2
taplly 在这个例子中不起作用,但在我的程序中起作用 抱歉
df2 <- data.frame(person.id, med.condition, cond.type)
df2.wide <- tapply(X = df2$medical.condition, INDEX = df2$person.id,
function(x){
ux <- unique(x)
c(ux, rep(x = NA, 9 - length (ux)))
})
df2.wide <- as.data.frame(do.call('rbind', df2.wide), stringsAsFactors = FALSE)
names(promis.b.temp) <- paste0('condition', 1:9)
cols
reshape2 很快意识到这行不通 图书馆(重塑2) df3 % 融化()%>% 唯一的() %>% 演员(person.id)
- 我是否正确处理了这个问题?
- 我在编写报告时是否会遇到必须使用字符串进行过滤的问题?
【问题讨论】:
标签: r