创建数据框时的 R 名称列

标签 r dataframe creation

我正在从数据框中获取列并使用它们来创建另一个数据框,但是名称不断变得困惑而不是保留原始名称。我如何避免这种情况?

> newDigit1 <- data.frame((security_id = rsPred1$security_id))
> head(newDigit1)
  X.security_id...rsPred1.security_id.
1                                    1
2                                    6
3                                    5
4                                    3
5                                    3
6                                    2

应该是这样的:
> newDigit1 <- data.frame((security_id = rsPred1$security_id))
> head(newDigit1)
                                security_id
1                                    1
2                                    6
3                                    5
4                                    3
5                                    3
6                                    2

最佳答案

那是因为你把括号加倍了 (( .
相比

dfr <- data.frame(x = 1:5)
#Case 1
data.frame(x = dfr$x)
#Case 2
data.frame((x = dfr$x))

在第 1 种情况下,x = dfr$x是传递给 data.frame 的名称-值对功能。

在情况 2 中,(x = dfr$x)返回一个没有名字的向量,所以 R 发明了一个临时向量,然后将该向量传递给 data.frame功能。

关于创建数据框时的 R 名称列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8792174/

相关文章:

r - 使用 RJSONIO 解析一行 JSON 对象的文本文件

python-3.x - Pandas 数据帧 read_excel : How to look for empty cells?

子动态创建的组件与父组件之间的 Angular4 通信

r - 将文本字符串转换为矩阵

R:引用类类型的引用类中的字段

r - 在 POSIXct 上附加日期时间对象

python - 将小节后缀值添加到 pandas 列值

python - Pandas - 不同列中两个索引之间的行总和

c++ - 如何在 while 循环中将结构变量的多个实例存储到可附加的 .txt 文件中

delphi - 如何处理复合组件中子组件的双击?