简体   繁体   English

R数据框中的动态列

[英]Dynamic columns in R dataframes

I'm playing with R dataframes, and trying to figure out how they work. 我正在玩R数据框,并试图弄清楚它们是如何工作的。 In the sample below, I'm trying to use a 1 row data frame to de-dup the elements of a vector. 在下面的示例中,我尝试使用1行数据帧来消除向量的元素。 I know there are much better ways to do this, like unique(), or use the hash library, etc, etc. This is more about learning how the dataframe works. 我知道有更好的方法来执行此操作,例如unique()或使用哈希库等。这更多地是关于学习数据帧的工作方式。

This first part works just fine, if the column name being added is a string: 如果要添加的列名是字符串,则第一部分工作正常。

> v = c(1, 2, 3, 10, 100, 50, 50, 100, 1, 2, 3, 10)
> d = data.frame(row.names=c('the row'))
> d
data frame with 0 columns and 1 rows
> for (x in v) { d[1,as.character(x)] = x}
> d
        1 2 3 10 100 50
the row 1 2 3 10 100 50

However, if I try to use a number as a column name, I get very strange behaviour: 但是,如果我尝试使用数字作为列名,则会出现非常奇怪的行为:

> e = data.frame(row.names=c('the row'))
> for (x in v) { e[1,x] = x}
Error in `[<-.data.frame`(`*tmp*`, 1, x, value = 10) : 
  new columns would leave holes after existing columns
> e
        V1 V2 V3
the row  1  2  3

First of all, where did 'V1', 'V2', and 'V3' come from? 首先,“ V1”,“ V2”和“ V3”来自何处? Secondly, why doesn't this work? 其次,为什么这不起作用? I mean, I can sort of work out that it's not happy that 10 is not the next number after 3, but other than that I don't know why this doesn't work. 我的意思是,我可以弄清楚,对于10不是3之后的下一个数字并不满意,但除此之外,我不知道为什么这不起作用。

Are columns only indexable as strings? 列只能索引为字符串吗? Other restrictions that are worth knowing about? 其他值得了解的限制?

Thanks in advance! 提前致谢!

To answer where the V1, V2, V3 names come from, check the source code of '[<-.data.frame' , line 139: 要回答V1, V2, V3名称的来源,请检查源代码'[<-.data.frame' ,第139行:

> deparse(`[<-.data.frame`)[139]
[1] "                new.cols <- paste0(\"V\", seq.int(from = nvars + "

As to why doesn't e[1,10] = 10 work when e has only 3 columns, well, it simply doesn't. 至于为什么当e只有3列时e[1,10] = 10不起作用,那根本就没有。 And if you think this contradicts your previous result ( d ), type d[,4] and see what happens. 如果您认为这与您先前的结果( d )相矛盾,请键入d[,4]并查看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM