保留Vector名称作为Dataframe列名。

时间:2021-05-07 16:20:36

In my code, I am filling the columns of a dataframe with vectors, as so:

在我的代码中,我用向量填充dataframe的列,如下所示:

df1[columnNum] <- barWidth

This works fine, except for one thing: I want the name of the vector variable (barWidth above) to be retained as the column header, one column at a time. Furthermore, I do not wish to use cbind. This slows the execution of my code down considerably. Consequently, I am using a pre-allocated dataframe.

这很好,除了一件事:我希望将向量变量的名称(上面的barWidth)保留为列标题,每次保留一列。此外,我不希望使用cbind。这大大降低了代码的执行速度。因此,我使用了一个预先分配的dataframe。

Can this be done in the vector-to-column assignment? If not, then how do I change it after the fact? I can't find the right syntax to do this with colNames().

这能在向量到列的分配中完成吗?如果不是,那么我该如何在事实发生后改变它呢?我找不到正确的语法来使用colNames()。

TIA

蒂雅

3 个解决方案

#1


1  

If you have the list of names of vectors you want to apply you could do:

如果你有你想要应用的向量的名字列表,你可以这样做:

namevec <- c(...,"barWidth"...,)
columnNums <- c(...,10,...)

df1[columnNums[i]] <- get(namevec[i])
names(df1)[columnNums[i]] <- namevec[i]

or even

甚至

columnNums <- c(barWidth=4,...)
for (i in seq_along(columnNums)) {
   df1[columnNums[i]] <- get(names(columnNums)[i])
}
names(df1)[columnNums] <- names(columnNums)

but the deeper question would be where this set of vectors is coming from in the first place: could you have them in a list all along?

但更深层的问题是,这一组向量是从哪里来的:你能一直把它们列在列表里吗?

#2


2  

It's being done by the [<-.data.frame function. It could conceivably be replaced by one that looked at the name of the argument but it's such a fundamental function I would be hesitant. Furthermore there appears to be an aversion to that practice signaled by this code at the top of the function definition:

它是由[<-.data.frame函数。它可以被一个看参数名字的函数代替但是它是一个基本的函数,我可能会犹豫。此外,在函数定义的顶部,似乎有一种对这个代码所表示的实践的厌恶:

> `[<-.data.frame`
function (x, i, j, value) 
{
    if (!all(names(sys.call()) %in% c("", "value"))) 
        warning("named arguments are discouraged")
    nA <- nargs()
    if (nA == 4L) {
 <snipped rest of rather long definition>

I don't know why that is there, but it is. Maybe you should either be thinking about using names<- after the column assignment, or using this method:

我不知道为什么会出现这种情况,但确实如此。也许你应该考虑在列赋值之后使用name <-,或者使用这种方法:

> dfrm["barWidth"] <- barWidth

> dfrm
  a V2 barWidth
1 a  1        1
2 b  2        2
3 c  3        3
4 d  4        4

This can be generalized to a list of new columns:

这可以概括为新列的清单:

dfrm <- data.frame(a=letters[1:4])
barWidth <- 1:4
newcols <- list(barWidth=barWidth, bw2 =barWidth)
dfrm[names(newcol)] <- newcol
dfrm
#
  a barWidth bw2
1 a        1   1
2 b        2   2
3 c        3   3
4 d        4   4

#3


0  

I'd simply use cbind():

我简单地使用cbind():

 df1 <- cbind( df1, barWidth )

which retains the name. It will, however, end up as the last column in df1

保留这个名字。然而,它最终将作为df1中的最后一列

#1


1  

If you have the list of names of vectors you want to apply you could do:

如果你有你想要应用的向量的名字列表,你可以这样做:

namevec <- c(...,"barWidth"...,)
columnNums <- c(...,10,...)

df1[columnNums[i]] <- get(namevec[i])
names(df1)[columnNums[i]] <- namevec[i]

or even

甚至

columnNums <- c(barWidth=4,...)
for (i in seq_along(columnNums)) {
   df1[columnNums[i]] <- get(names(columnNums)[i])
}
names(df1)[columnNums] <- names(columnNums)

but the deeper question would be where this set of vectors is coming from in the first place: could you have them in a list all along?

但更深层的问题是,这一组向量是从哪里来的:你能一直把它们列在列表里吗?

#2


2  

It's being done by the [<-.data.frame function. It could conceivably be replaced by one that looked at the name of the argument but it's such a fundamental function I would be hesitant. Furthermore there appears to be an aversion to that practice signaled by this code at the top of the function definition:

它是由[<-.data.frame函数。它可以被一个看参数名字的函数代替但是它是一个基本的函数,我可能会犹豫。此外,在函数定义的顶部,似乎有一种对这个代码所表示的实践的厌恶:

> `[<-.data.frame`
function (x, i, j, value) 
{
    if (!all(names(sys.call()) %in% c("", "value"))) 
        warning("named arguments are discouraged")
    nA <- nargs()
    if (nA == 4L) {
 <snipped rest of rather long definition>

I don't know why that is there, but it is. Maybe you should either be thinking about using names<- after the column assignment, or using this method:

我不知道为什么会出现这种情况,但确实如此。也许你应该考虑在列赋值之后使用name <-,或者使用这种方法:

> dfrm["barWidth"] <- barWidth

> dfrm
  a V2 barWidth
1 a  1        1
2 b  2        2
3 c  3        3
4 d  4        4

This can be generalized to a list of new columns:

这可以概括为新列的清单:

dfrm <- data.frame(a=letters[1:4])
barWidth <- 1:4
newcols <- list(barWidth=barWidth, bw2 =barWidth)
dfrm[names(newcol)] <- newcol
dfrm
#
  a barWidth bw2
1 a        1   1
2 b        2   2
3 c        3   3
4 d        4   4

#3


0  

I'd simply use cbind():

我简单地使用cbind():

 df1 <- cbind( df1, barWidth )

which retains the name. It will, however, end up as the last column in df1

保留这个名字。然而,它最终将作为df1中的最后一列