我的数据框在R中看起来有点像这样:
D = data.frame(countrycode = c(2, 2, 2, 3, 3, 3),
year = c(1980, 1991, 2013, 1980, 1991, 2013),
pop90 = c(1, 1, 1, 2, 2, 2),
pop00 = c(3, 3, 3, 4, 4, 4),
pop10 = c(5, 5, 5, 6, 6, 6))
期望的输出:
Res = data.frame(countrycode = c(2, 2, 2, 3, 3, 3),
year = c(1980, 1991, 2013, 1980, 1991, 2013),
popcombined = c(1, 3, 5, 2, 4, 6))
我想将pop90,pop00和pop10组合成一列,其中1980-1990年将反映pop90的价值,1991-2000年将反映pop00的价值,2001-2013年将反映pop10的价值。我怎样才能做到这一点?我已经尝试了合并功能,但我无法设置多年来反映我上面列出的条件。
答案 0 :(得分:5)
您可以使用row/col
索引
popcombined <- D[3:5][cbind(1:nrow(D),findInterval(D$year,
c(-Inf, 1990, 2000, Inf)))]
cbind(D[1:2], popcombined)
# countrycode year popcombined
#1 2 1980 1
#2 2 1991 3
#3 2 2013 5
#4 3 1980 2
#5 3 1991 4
#6 3 2013 6
答案 1 :(得分:1)
您可以使用cut
并执行以下操作:
library(plyr)
adply(D, 1, function(u){
transform(u[,1:2],
pop = cut(u$year, c(1980, 1990, 2000, 2013), label=tail(unlist(u),3),include.lowest=T))
})
答案 2 :(得分:0)
我将所有不需要的数据从包NA
设置为melt
和reshape2
:
## Set NA's for every decade
library(Hmisc)
D[D$year %nin% 1980:1989,]$pop90 <- NA
D[D$year %nin% 1990:1999,]$pop00 <- NA
D[D$year %nin% 2000:2013,]$pop10 <- NA
# Melt data.frame
library(reshape2)
D.new <- melt(D, id.vars = c("countrycode", "year"),
value.name = "popcombined")
# Some minor stuff
D.new <- na.omit(D.new)
D.new <- D.new[,-3]
D.new <- arrange(D.new, countrycode)
# Check my data against your result
> D.new == Res
countrycode year popcombined
[1,] TRUE TRUE TRUE
[2,] TRUE TRUE TRUE
[3,] TRUE TRUE TRUE
[4,] TRUE TRUE TRUE
[5,] TRUE TRUE TRUE
[6,] TRUE TRUE TRUE
答案 3 :(得分:0)
使用基本索引:
D[D$year>=1980 & D$year<1990 , "popcombined" ] <- D[D$year>=1980 & D$year<1990, "pop90" ]
D[D$year>=1990 & D$year<2000 , "popcombined" ] <- D[D$year>=1990 & D$year<2000, "pop00" ]
D[D$year>=2000 , "popcombined" ] <- D[D$year>=2000 , "pop10" ]
使用with
:
D$popcombined2 <-NA
D$popcombined2 <- with(D, ifelse( year>=1980 & year<1990, pop90, popcombined2 ))
D$popcombined2 <- with(D, ifelse( year>=1990 & year<2000, pop00, popcombined2 ))
D$popcombined2 <- with(D, ifelse( year>=2000 , pop10, popcombined2 ))
#> D
# countrycode year pop90 pop00 pop10 popcombined popcombined2
#1 2 1980 1 3 5 1 1
#2 2 1991 1 3 5 3 3
#3 2 2013 1 3 5 5 5
#4 3 1980 2 4 6 2 2
#5 3 1991 2 4 6 4 4
#6 3 2013 2 4 6 6 6