1

1)我想在 Gnu R 中使用此处的数据集进行子集操作,以生成仅包含巴西、时间和有关收入份额的所有系列名称的结果数据框(例如“最低 10% 持有的收入份额”、“ “Income share by minimum 20%”等)。总共会有7个关于Income Share的系列名称。

我尝试了以下命令,但不能子集多个“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"))
4

2 回答 2

3

查看您的数据,这实际上可能最容易grepl用于帮助子集化。

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

将其全部包装起来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”中有七个因子级别。

于 2015-01-14T17:17:46.817 回答
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),]
于 2015-01-14T17:13:47.743 回答