使用R中的for循环重新评估许多观测值
我有一个数据集,我在其中查看国家的纵向数据使用R中的for循环重新评估许多观测值,r,R,我有一个数据集,我在其中查看国家的纵向数据 master.set <- data.frame( Country = c(rep("Afghanistan", 3), rep("Albania", 3)), Country.ID = c(rep("Afghanistan", 3), rep("Albania", 3)), Year = c(2015, 2016, 2017, 2015, 2016, 2017), Happiness.Score = c(3.575, 3.360
master.set <- data.frame(
Country = c(rep("Afghanistan", 3), rep("Albania", 3)),
Country.ID = c(rep("Afghanistan", 3), rep("Albania", 3)),
Year = c(2015, 2016, 2017, 2015, 2016, 2017),
Happiness.Score = c(3.575, 3.360, 3.794, 4.959, 4.655, 4.644),
GDP.PPP = c(1766.593, 1757.023, 1758.466, 10971.044, 11356.717, 11803.282),
GINI = NA,
Status = 2,
stringsAsFactors = F
)
> head(master.set)
Country Country.ID Year Happiness.Score GDP.PPP GINI Status
1 Afghanistan Afghanistan 2015 3.575 1766.593 NA 2
2 Afghanistan Afghanistan 2016 3.360 1757.023 NA 2
3 Afghanistan Afghanistan 2017 3.794 1758.466 NA 2
4 Albania Albania 2015 4.959 10971.044 NA 2
5 Albania Albania 2016 4.655 11356.717 NA 2
6 Albania Albania 2017 4.644 11803.282 NA 2
我收到了这个错误:
Error in `[[<-.data.frame`(`*tmp*`, Country.ID[i], value = logical(0)) :
replacement has 0 rows, data has 460
“[[中的错误可能类似于
master.set$Country.ID 1阿富汗阿富汗2015 3.575 1766.593 NA 2
#>2阿富汗阿富汗2016 3.360 1757.023 NA 2
#>3阿富汗2017 3.794 1758.466 NA 2
#>4阿尔巴尼亚阿尔巴尼亚2015 4.959 10971.044 NA 2
#>5阿尔巴尼亚阿尔巴尼亚2016 4.655 11356.717 NA 2
#>6阿尔巴尼亚2017 4.644 11803.282 NA 2
#>Country.ID2
#> 1 1
#> 2 1
#> 3 1
#> 4 2
#> 5 2
#> 6 2
库(dplyr)
df%变异(国家/地区id=集团指数(,.dots=“国家”))
视图(df1)
您是否尝试将列转换为因子
?如果之后需要将其转换为整数,您可以轻松地将转换为.integer
。如果您需要特定国家的特定数字,请使用as.integer(因子(master.set$Country.ID,levels=correct\u order\u of\u countries))
。这是@r2evans公平的回答。:-)看看postTrue下面的评论!我没有注意到这一点;这里有另一个答案来区分我的和他们的。
Error in `[[<-.data.frame`(`*tmp*`, Country.ID[i], value = logical(0)) :
replacement has 0 rows, data has 460
library(dplyr)
df<-data.frame("Country"=c("Afghanistan","Afghanistan","Afghanistan","Albania","Albania","Albania"),
"Year"=c(2015,2016,2017,2015,2016,2017),
"Happiness.Score"=c(3.575,3.360,3.794,4.959,4.655,4.644),
"GDP.PPP"=c(1766.593,1757.023,1758.466,10971.044,11356.717,11803.282),
"GINI"=NA,
"Status"=rep(2,6))
df1<-df %>% arrange(Country) %>% mutate(Country_id = group_indices_(., .dots="Country"))
View(df1)