【问题标题】:How to create tidy data with a Dataset where values are duplicated over many rows如何使用在多行中重复值的数据集创建整洁的数据
【发布时间】: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


    【解决方案1】:

    我真的不明白这个问题。您的数据已经看起来很“整洁”。

    我注意到的两件事是 (1) 重复值(可能需要也可能不需要)和 (2) 每个人和医疗状况都没有唯一的 ID。

    如果你想要一个用逗号分隔的长字符串(我认为以后很难处理),你可以只通过前两列中的唯一值进行聚合,如下所示:

    library(data.table)
    as.data.table(unique(df[1:2]))[, paste(med.condition, collapse = ","), by = person.id]
    #    person.id                                                                                  V1
    # 1:      1017                                                                                  NA
    # 2:      1018 Allergic rhinitis,Atopic Dermatitis,Developmental Speech,Eye Condition,Speech Delay
    # 3:      1019                                                                Allergic Reaction,NA
    # 4:      1020                                                                        Eczema,Obese
    

    如果您想轻松获得每个人的顺序 ID,可以使用我的“splitstackshape”包中的getanID

    library(splitstackshape)
    getanID(as.data.table(unique(df[1:2]))
    

    如果需要,您可以使用 dcast.data.table 转换为宽格式,如下所示:

    library(splitstackshape)
    dcast.data.table(getanID(as.data.table(unique(df[1:2])), "person.id"), 
                     person.id ~ .id, value.var = "med.condition", 
                     fun.aggregate = function(x) paste(x, collapse = ","))
    #    person.id                 1                 2                    3             4            5
    # 1:      1017                NA                                                                  
    # 2:      1018 Allergic rhinitis Atopic Dermatitis Developmental Speech Eye Condition Speech Delay
    # 3:      1019 Allergic Reaction                NA                                                
    # 4:      1020            Eczema             Obese                                                
    

    【讨论】:

    • 我独有的问题是 person.id 是我所有数据的主键,其中包括我们使用的其他几种不同的评估工具。所以我没有添加新 seq.ids 的选项。我也不喜欢长逗号分隔的值。我只是不知道我在继续前进时的选择是什么,然后看看我以后是否会遇到麻烦。 dplyr filter(df, contains == Developmental Speech) 可能对我有帮助
    【解决方案2】:

    您可以使用基本的reshape() 函数来执行此操作,如果您只需为每个观察添加一个“时间”指示器(您可以使用ave() 轻松完成此操作)。如果你跑

    reshape(
        transform(
            unique(df[, c("person.id","med.condition")]), 
            time=ave(as.numeric(person.id), person.id, FUN=seq_along)
        ), 
        idvar="person.id", 
        v.names="med.condition",
        direction="wide")
    

    你会得到

    person.id   med.condition.1 med.condition.2 med.condition.3 med.condition.4 med.condition.5
    1017    NA  NA  NA  NA  NA
    1018    Allergic rhinitis   Atopic Dermatitis   Developmental Speech    Eye Condition   Speech Delay
    1019    Allergic Reaction   NA  NA  NA  NA
    1020    Eczema  Obese   NA  NA  NA
    

    【讨论】:

      【解决方案3】:

      您的数据框采用所谓的“长”格式,您希望将其重塑为“宽”格式。试试下面:

      require(reshape2)
      df.new <- reshape(df,idvar='person.id',timevar='cond.type',direction='wide')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 2022-01-07
        • 1970-01-01
        • 2015-06-26
        • 1970-01-01
        相关资源
        最近更新 更多