复杂的子集数据集到数据帧

时间:2015-01-14 16:57:58

标签: r subset reshape reshape2 melt

1)我想在Gnu R中使用数据集here进行子集操作,以便仅使用巴西,时间和所有关于收入的系列名称生成数据框分享(如“最低10%持有的收入份额”,“最低20%持有的收入份额”等)。总共会有7个关于收益分享的系列名称。

我尝试了以下命令,但不能将多个“Series.Name”子集:

test <- melt(subset(WDI, subset = Series.Name == "Income share held by lowest 10%", select = -c(Time.Code, Series.Code, Argentina, Canada, Chile, Colombia, Mexico, USA, Venezuela)), id.vars = c("Series.Name", "Time"))

2)在另外的第二步中,我想删除所有包含NA值的行。

我使用的完整代码如下:

WDI <- read.csv(https://dl.dropboxusercontent.com/u/109495328/WDI_Data_final.csv, na.strings = "..")
library(reshape)
library(reshape2)
WDI <- rename(WDI, (c(Argentina..ARG.="Argentina", Brazil..BRA.="Brazil", Canada..CAN.="Canada", Chile..CHL.="Chile", Colombia..COL.="Colombia", Mexico..MEX.="Mexico", United.States..USA.="USA", Venezuela..RB..VEN.="Venezuela")))
income_brazil_long <- melt(subset(WDI, subset = Series.Name == "Income share held by lowest 10%", select = -c(Time.Code, Series.Code, Argentina, Canada, Chile, Colombia, Mexico, USA, Venezuela)), id.vars = c("Series.Name", "Time"))

2 个答案:

答案 0 :(得分:3)

查看您的数据,使用grepl帮助进行子集化实际上这可能是最简单的。

我们使用grepl在“Series.Name”列中搜索包含字符串“Income share held”的所有行。这会创建一个逻辑向量,指示我们想要的行。我们想要的列是第一个,第三个和第六个。

将所有内容全部包含在na.omit中以删除任何包含NA值的行。

WDI_Brazil <- na.omit(WDI[grepl("Income share held", WDI$Series.Name), 
                          c(1, 3, 6)]) 

数据已经“很长”,因此无需meltdata.frame看起来像什么?

summary(WDI_Brazil)
#                            Series.Name      Time       Brazil..BRA.   
#  Income share held by fourth 20% :28   Min.   :1981   Min.   : 0.600  
#  Income share held by highest 10%:28   1st Qu.:1988   1st Qu.: 2.895  
#  Income share held by highest 20%:28   Median :1996   Median :10.320  
#  Income share held by lowest 10% :28   Mean   :1996   Mean   :20.948  
#  Income share held by lowest 20% :28   3rd Qu.:2004   3rd Qu.:43.797  
#  Income share held by second 20% :28   Max.   :2012   Max.   :67.310  
#  (Other)                         :28                                  
table(droplevels(WDI_Brazil$Series.Name))
# 
#  Income share held by fourth 20% Income share held by highest 10% Income share held by highest 20% 
#                               28                               28                               28 
#  Income share held by lowest 10%  Income share held by lowest 20%  Income share held by second 20% 
#                               28                               28                               28 
#   Income share held by third 20% 
#                               28 

请注意,“Series.Name”中有七个因子级别,正如预期的那样。

答案 1 :(得分:1)

嗯,您可以使用base函数执行您要查找的内容。

WDI <- read.csv("WDI_Data_final.csv", header=T, na.strings="..")

# The colnames are strange from the file so reset for clarity
colnames(WDI) <- c("Series.Name", "Series.Code", "Time","Time.Code","Argentina",
                   "Brazil", "Canada", "Chile", "Colombia","Mexico",
                   "USA", "Venezuela")

# do the subsetting
test <- with(WDI, 
             WDI[Series.Name=="Income share held by lowest 10%",
                 c("Brazil","Time", "Series.Name")])

# if you want more, use %in% and specify the Series.Names you care about
test <- with(WDI, 
             WDI[Series.Name %in% c("Income share held by lowest 10%", 
                                    "Income share held by lowest 20%"),
                 c("Brazil","Time", "Series.Name")])

# if you want all the 'income shares', the grepl solution above  by
# Ananda is the most concise.

# you can then use reshape2::melt
melted_test <- melt(test, id.vars=c("Series.Name", "Time"))

要删除NA,请使用complete.cases

test[complete.cases(test),]