簡體   English   中英

如何將列與R中的條件結合在一起?

[英]How do you combine columns with conditions in R?

我在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的值。 我怎樣才能做到這一點? 我已經嘗試過合並功能,但是無法設置年份來反映我上面列出的條件。

您可以使用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

您可以使用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))
})

我所有不需要的數據設定為NAmelt從包編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

使用基本索引:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM