【问题标题】:How can I tidy this data set in r studio step by step?如何在 r studio 中逐步整理这些数据集?
【发布时间】:2019-11-01 00:03:22
【问题描述】:

我希望使用 R Studio 整理数据集。我想知道如何逐步清理这个数据集。 数据如下所示:

enter image description here

但我希望它们看起来像这样:

enter image description here

知道我需要什么 r 代码吗?

【问题讨论】:

    标签: r rstudio tidyr


    【解决方案1】:

    我认为这可以满足您的需求。

        #Creating the dataframe
    country=c("Aghanistan","Aghanistan","Aghanistan","Brazil","Brazil","Brazil","China","China","China")
        Indicator=c("ind1","ind2","ind3","ind1","ind2","ind3","ind1","ind2","ind3")
        `1999`=c(745,19987071,0.373,NA,NA,NA,NA,NA,NA)
        `2000`=c(2666,20595360,1.29,NA,NA,NA,NA,NA,NA)
        df<-as.data.frame(cbind(country,Indicator,`1999`,`2000`))
    
        library(tidyr)
        df.long<-gather(df,Year,measurement,`1999`:`2000`,factor_key = T)#get the years right
        df.final<-spread(df.long,Indicator,measurement)#reformat for final
        df.final#scope the result
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      相关资源
      最近更新 更多