【问题标题】:In R, how to select a year-specific datum from multiple year columns?在 R 中,如何从多个年份列中选择特定年份的数据?
【发布时间】:2013-08-12 19:33:33
【问题描述】:

我有一个数据集,对于其中的每一列,我想选择一个仅基于观察年份的响应数据。我的数据集版本只有前四列;我想使用 R 语法创建第五列。在第五列 yr.response 中,我只想要与正确年份对应的数据。如果 ifelse() 语句(例如,如果 year==2000,则从 y2000 列中选择等),我正在考虑进行一些迭代,但无法使其正常工作。我应该补充一点,实际数据集大约有 30 年和 19K 行,所以我希望能够自动化。

year<-c(2000,2000,2001,2002)
y2000<-c(65,43,42,74)
y2001<-c(98,93,987,948)
y2002<-c(875,983,776,736)
yr.response<-c(65,43,987,736)
x<-data.frame(cbind(year, y2000,y2001,y2002,yr.response))

【问题讨论】:

    标签: r


    【解决方案1】:
    my.df <- data.frame(year, y2000, y2001, y2002)
    rownames(my.df) <- as.character(rownames(my.df))
    my.df$yr.respnose <- my.df[cbind(rownames(my.df), paste0("y", my.df$year))]
    

    【讨论】:

    • 简洁的解决方案。我不知道您可以从带有矩阵的数据框中选择元素。
    【解决方案2】:

    您可以使用 switch 语句和 for 循环:

    # Define a function to return which column the year refers to
    col_finder <- function(r1) {
      switch(r1,
             "2000"=1,
             "2001"=2,
             "2002"=3) 
    }
    
    # Initiallize a new column
    x$yr.response2 <- NA
    
    # Switch statements are not vectorized, so run this in a for loop
    for(i in 1:nrow(x)) {
      cmn <- col_finder(as.character(x[i, "year"]))
      x[i, "yr.response2"] <- x[i, cmn]
    }
    

    【讨论】:

      【解决方案3】:
      year<-c(2000,2000,2001,2002)
      y2000<-c(65,43,42,74)
      y2001<-c(98,93,987,948)
      y2002<-c(875,983,776,736)
      yr.response<-NA
      x<-data.frame(cbind(year, y2000,y2001,y2002,yr.response))
      
      for(year in x[,"year"]){
        x$yr.response[which(x$year==year)] <- x[which(x$year==year),grep(year,colnames(x))]
      }
      

      给予:

      > x
        year y2000 y2001 y2002 yr.response
      1 2000    65    98   875          65
      2 2000    43    93   983          43
      3 2001    42   987   776         987
      4 2002    74   948   736         736
      

      我仍然不确定 y2001 列中的 98 是什么意思,其中年份列为 2000,但此代码复制了您的示例 yr.response 列。

      此示例假定您有唯一的列。换句话说,您不能有两个 y2000 列。

      【讨论】:

        【解决方案4】:

        这是我使用 Map 函数的解决方案,因此不需要 R for loopmyrow&lt;-as.list(df$year) # 将年份转换为列表以供输入Map

        myout1<-Map(function(x) df[df$year==as.numeric(x),paste0("y",x)], myrow)# use to generate the output
        

        #将其转换为数据帧

        myout2<-do.call(rbind,myout1)
        

        #如果我们在年份列下的每一年有多个观察值,这里需要一个小技巧(这里每年最多有两个观察值)

        myout2[which(!(myout2[,1]==myout2[,2])[1]==TRUE),2]<-myout2[which(!(myout2[,1]==myout2[,2])[1]==TRUE),1]
        
        df$yr.response<-myout2[,2]
        
        
        > df
          year y2000 y2001 y2002 yr.response
        1 2000    65    98   875          65
        2 2000    43    93   983          43
        3 2001    42   987   776         987
        4 2002    74   948   736         736
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-07-06
          • 2015-04-03
          • 1970-01-01
          • 1970-01-01
          • 2014-03-29
          • 1970-01-01
          相关资源
          最近更新 更多