【发布时间】:2018-07-30 11:08:00
【问题描述】:
(添加了可重现的示例)。考虑以下示例:
df <- as.data.frame(matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3))
# V1 V2 V3
# 1 1 3 12
# 2 2 11 13
rownames(df) # "1" "2"
dput(df)
# structure(list(V1 = c(1, 2), V2 = c(3, 11), V3 = c(12, 13)), .Names = c("V1",
# "V2", "V3"), row.names = c(NA, -2L), class = "data.frame")
dput 的row.names(即NA、-2L)与rownames()(即1、2)返回的不一致。为什么?
PS1:有人可以添加dput标签吗,我没有足够的声誉来做它(即1500分)?
PS2:What's the difference between row.names() and attributes$row.names? 也有类似的问题。在链接中,使用了row.names 和$row.names(都带有点)。然而,在这个问题中,出现了rownames 和row.names(无点和点)。也就是说,给定的链接很有帮助,并且在很大程度上解决了问题。
【问题讨论】:
-
见
?.set_row_names。 -
@mt1022
.set_row_names(as.data.frame(matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3)))[[1]] # NA所以,现在我明白了(NA, -2L)中的NA来自哪里。谢谢。 -
没有。这不是
NA的来源。您应该尝试.set_row_names(nrow(df)),其中df是您的data.frame(即as.data.frame(matrix(c(1,2,3, 11,12,13), nrow = 2, ncol=3)))。你会得到准确的c(NA, -2L)。 -
@mt1022,是的,你是对的:
.set_row_names(nrow(df)) # NA -2同时给出两个组件(即NA -2),我只给出了NA!