(在R中)将向量中的元数据添加到数据框的一组列中? [英] (in R) Add metadata from a vector to a set of columns of a dataframe?
本文介绍了(在R中)将向量中的元数据添加到数据框的一组列中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用我创建的字符向量中的值作为数据帧中一组变量的标签属性.
I would like to use values from a character vector that I created as label attributes for a set of variables in a dataframe.
我认为这个简单的解决方案应该可以,但是不能解决
I thought this simple solution should work, yet it does not:
x <- rep("text", time=19) %>%
paste(1:19, sep = " ") #character vector with names of label attributes I want
attr(mydataframe[var_names], "label") <- x #var_names and x have the same length
感谢您的帮助!
推荐答案
Hmisc支持列标签.使用具有8列的内置数据框 anscombe
:
Hmisc supports column labels. Using the built in data frame anscombe
having 8 columns:
library(Hmisc)
x <- paste("label", i)
for(i in seq_along(anscombe)) label(anscombe[[i]]) <- x[i]
Label(anscombe)
给予:
label(x1) <- 'label 1'
label(x2) <- 'label 2'
label(x3) <- 'label 3'
label(x4) <- 'label 4'
label(y1) <- 'label 5'
label(y2) <- 'label 6'
label(y3) <- 'label 7'
label(y4) <- 'label 8'
这篇关于(在R中)将向量中的元数据添加到数据框的一组列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文