【问题标题】:How to copy attributes from one data frame to another or to re-assign attributes to a freshly transposed data frame - R如何将属性从一个数据帧复制到另一个数据帧或将属性重新分配给新转置的数据帧 - R
【发布时间】:2019-08-16 04:11:52
【问题描述】:

转置数据后,我想重新分配已删除的属性。这也适用于将属性从一个数据帧复制到另一个数据帧。或者在变异后复制属性,等等,它们被丢弃的地方。

 library(reshape2)

 df <- data.frame(id = c(1,2,3,4,5), 
                  time = c(11, 22,33,44,55),
                  c  = c(1,2,3,5,5),
                  d = c(4,2,5,4,NA))

attr(df$id,"label")<- "label"
attr(df$time,"label")<- "label2"
attr(df$c,"label")<- "something here"
attr(df$d,"label")<- "count of something"
str(df)

 str(df)
 data.frame':   5 obs. of  4 variables:
 $ id  : num  1 2 3 4 5
  ..- attr(*, "label")= chr "label"
 $ time: num  11 22 33 44 55
  ..- attr(*, "label")= chr "label2"
 $ c   : num  1 2 3 5 5
  ..- attr(*, "label")= chr "something here"
 $ d   : num  4 2 5 4 NA
  ..- attr(*, "label")= chr "count of something"

投射到广角

dfwide<- recast(df,id~variable +time, 
            id.var = c("id","time"))

通常的属性丢失消息:

   Warning message:
     attributes are not identical across measure variables; they will be dropped 

 str(dfwide)
'data.frame':   5 obs. of  11 variables:
 $ id  : num  1 2 3 4 5
 $ c_11: num  1 NA NA NA NA
 $ c_22: num  NA 2 NA NA NA
 $ c_33: num  NA NA 3 NA NA
 $ c_44: num  NA NA NA 5 NA
 $ c_55: num  NA NA NA NA 5
 $ d_11: num  4 NA NA NA NA
 $ d_22: num  NA 2 NA NA NA
 $ d_33: num  NA NA 5 NA NA
 $ d_44: num  NA NA NA 4 NA
 $ d_55: num  NA NA NA NA NA

使用mostattributes 可以在数据帧之间复制属性,但是对于许多列名的迭代,我无法弄清楚或考虑如何以不同的方式有效地映射它,一个一个地保存。

 mostattributes(dfwide$c_11)<-attributes(df$c)
 mostattributes(dfwide$c_22)<-attributes(df$c)
 > str(dfwide)
 'data.frame':  5 obs. of  11 variables:
  $ id  : num  1 2 3 4 5
  $ c_11: num  1 NA NA NA NA
  ..- attr(*, "label")= chr "something here"
  $ c_22: num  NA 2 NA NA NA
  ..- attr(*, "label")= chr "something here"
  $ c_33: num  NA NA 3 NA NA

我试图让它自动化但失败了(所有 c 应该有相同的标签,d 应该有相同的标签):

#extract arguments
dlist<-enframe(names(df))%>%
   slice(-1,-2)%>%
   pull(., value)
 dlist

 dlistw<-enframe(names(dfwide))%>%
  slice(-1)%>%
  pull(., value)
 dlistw

#function
mostatt<- function(var1, var2) {
  mostattributes(dfwide[[var1]])<<-attributes(df[[var2]])
}

mapply(mostatt,dlistw,dlist)
str(dfwide)

'data.frame':   5 obs. of  11 variables:
 $ id  : num  1 2 3 4 5
 $ c_11: num  1 NA NA NA NA
  ..- attr(*, "label")= chr "something here"
 $ c_22: num  NA 2 NA NA NA
  ..- attr(*, "label")= chr "count of something"
 $ c_33: num  NA NA 3 NA NA
  ..- attr(*, "label")= chr "something here"
 $ c_44: num  NA NA NA 5 NA
  ..- attr(*, "label")= chr "count of something"
 $ c_55: num  NA NA NA NA 5
  ..- attr(*, "label")= chr "something here"
 $ d_11: num  4 NA NA NA NA
  ..- attr(*, "label")= chr "count of something"
 $ d_22: num  NA 2 NA NA NA
  ..- attr(*, "label")= chr "something here"
 $ d_33: num  NA NA 5 NA NA
  ..- attr(*, "label")= chr "count of something"
 $ d_44: num  NA NA NA 4 NA
  ..- attr(*, "label")= chr "something here"
 $ d_55: num  NA NA NA NA NA
  ..- attr(*, "label")= chr "count of something"

我认为使用tidyselect starts_with 可能值得一试,但不确定如何合并它。任何建议,将不胜感激。谢谢!

【问题讨论】:

    标签: r attr purrr reshape2 tidyselect


    【解决方案1】:

    这是一个选项:

    for(i in (setdiff(colnames(df), "id"))){
      for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
          mostattributes(dfwide[[x]]) <- attributes(df[[i]])
    }
    mostattributes(dfwide$id) <- attributes(df$id) 
    

    因为d 包含在id 中,所以最后需要重写id。 如果将d 更改为e 会更简单:

    df <- data.frame(id = c(1,2,3,4,5), 
                     time = c(11, 22,33,44,55),
                     c  = c(1,2,3,5,5),
                     e = c(4,2,5,4,NA))
    
    
    attr(df$id,"label")<- "label"
    attr(df$time,"label")<- "label2"
    attr(df$c,"label")<- "something here"
    attr(df$e,"label")<- "count of something"
    str(df)
    
    dfwide<- recast(df,id~variable +time, 
                    id.var = c("id","time"))
    
    for(i in (colnames(df))){
      for(x in colnames(dfwide)[(grepl(i, colnames(dfwide)))])
        mostattributes(dfwide[[x]]) <- attributes(df[[i]])
    }
    

    【讨论】:

    • 这是一个不错的选择。谢谢你的分享。我没有意识到可以像这样 grep/grepl。我想知道是否有办法在语句中添加一个正则表达式克拉 ^ 来表示模式的起始位置。我会玩弄它的。但是因为它是这个解决方案对我有用,因为我拥有的数据框中的 colnames 更长,并且 grepl 将在这些字符串而不是单个字符上工作。再次感谢您!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2016-09-15
    • 1970-01-01
    • 2021-04-03
    • 2020-08-20
    • 2015-09-08
    • 1970-01-01
    相关资源
    最近更新 更多