如何将属性从一个数据框复制到另一个数据框或将属性重新分配给新转置的数据框 - R
How to copy attributes from one data frame to another or to re-assign attributes to a freshly transposed data frame - R
转置数据后,我想重新分配删除的属性。这也适用于将属性从一个数据框复制到另一个数据框。或者在 mutates 等之后复制属性,在它们被删除的地方。
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
可能值得一试,但不确定如何合并它。任何建议,将不胜感激。谢谢!
这是一个选项:
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]])
}
转置数据后,我想重新分配删除的属性。这也适用于将属性从一个数据框复制到另一个数据框。或者在 mutates 等之后复制属性,在它们被删除的地方。
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
可能值得一试,但不确定如何合并它。任何建议,将不胜感激。谢谢!
这是一个选项:
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]])
}