重塑R中的宽数据:将两行转换为列

时间:2018-10-05 17:59:56

标签: r dataframe reshape reshape2 melt

如何在R中转置此数据集?见下文:

我下载了一个看起来像这样的数据集(日期从2016年回溯至1975年):

           V1               V2               V3               V4               V5
1                         2016             2016             2016             2015
4     Country       Both-sexes             Male           Female       Both-sexes
5 Afghanistan 23.4 [22.0-24.8] 22.6 [20.1-25.1] 24.1 [23.0-25.3] 23.3 [21.9-24.6]
6     Albania 26.7 [25.8-27.5] 27.0 [25.8-28.2] 26.3 [25.0-27.6] 26.6 [25.8-27.4]
7     Algeria 25.5 [24.5-26.5] 24.7 [23.4-26.1] 26.4 [24.9-27.8] 25.5 [24.5-26.4]
8     Andorra 26.7 [24.6-28.7] 27.3 [24.8-29.8] 26.1 [22.8-29.5] 26.7 [24.7-28.7]

我需要将年和性别行(当前编号为行1和4)分成几列。这就是我想要的:

1 Country Year Sex Rate 2 Afghanistan 2016 Both-sexes 23.4 3 Afghanistan 2016 Male 22.6 3 Afghanistan 2016 Female 24.1 4 Afghanistan 2015 Both-sexes 23.3

...,并且数据集中所有国家/地区的行全年都在继续。

这就是我试图到达那里的方法:

cfile <- read.csv(file= "countries-BMI.csv", header = F)


#removed second two rows that have unnecessary info
countries_data <- cfile[-c(2,3), ]

molten_countries_data <- melt(countries_data, id=c("V1"))

。这是我的结果-head(molten_countries_data)

           V1 variable            value
1                   V2             2016
2     Country       V2       Both-sexes
3 Afghanistan       V2 23.4 [22.0-24.8]
4     Albania       V2 26.7 [25.8-27.5]
5     Algeria       V2 25.5 [24.5-26.5]
6     Andorra       V2 26.7 [24.6-28.7]

不是我想要的!请帮忙。

1 个答案:

答案 0 :(得分:1)

由于@ Dave2e给出了先合并前两行的提示,所以我知道了。这就是我最终要做的事情:

library(reshape2)
library(tidyr)

#load data frame without first two rows
cdata <- read.csv("countries-BMI.csv", skip = 2, header = F)

#create header by combining top two rows
headers <- read.csv("countries-BMI.csv", nrows=2, header=FALSE)
headers_names <- sapply(headers,paste,collapse="_")

#add the new header to data frame
names(cdata) <- headers_names

#transpose the "wide data" to make it tidy/long
longdata <- melt(cdata, id.vars = c("_Country"))

#separate the year and sex columns
countriesBMI2 <- separate(data = longdata, col = variable, into = c("Year", "Sex"), sep = "_")

我的结果:head(countriesBMI2)

             _Country Year        Sex            value
1         Afghanistan 2016 Both-sexes 23.4 [22.0-24.8]
2             Albania 2016 Both-sexes 26.7 [25.8-27.5]
3             Algeria 2016 Both-sexes 25.5 [24.5-26.5]
4             Andorra 2016 Both-sexes 26.7 [24.6-28.7]
5              Angola 2016 Both-sexes 23.3 [21.2-25.6]
6 Antigua and Barbuda 2016 Both-sexes 26.7 [24.6-28.8]