34

我有下面的 cbind 代码行,但我每次都会收到一条警告消息。尽管代码仍然可以正常运行,但有什么办法可以解决警告吗?

dateset = subset(all_data[,c("VAR1","VAR2","VAR3","VAR4","VAR5","RATE1","RATE2","RATE3")])
dateset = cbind(dateset[c(1,2,3,4,5)],stack(dateset[,-c(1,2,3,4,5)]))

警告:

Warning message:
   In data.frame(..., check.names = FALSE) :
        row names were found from a short variable and have been discarded

提前致谢!

4

1 回答 1

48

我猜你data.framerow.names

A <- data.frame(a = c("A", "B", "C"), 
                b = c(1, 2, 3), 
                c = c(4, 5, 6), 
                row.names=c("A", "B", "C"))

cbind(A[1], stack(A[-1]))
#   a values ind
# 1 A      1   b
# 2 B      2   b
# 3 C      3   b
# 4 A      4   c
# 5 B      5   c
# 6 C      6   c
# Warning message:
# In data.frame(..., check.names = FALSE) :
#   row names were found from a short variable and have been discarded

这里发生的情况是,由于默认情况下您不能row.names在 a中复制,并且由于在将第一列回收到堆叠列的相同行数时,data.frame您在任何时候都没有告诉 R 复制,R 只是丢弃.row.namesrow.names

与类似data.frame但没有的比较row.names

B <- data.frame(a = c("A", "B", "C"), 
                b = c(1, 2, 3), 
                c = c(4, 5, 6))

cbind(B[1], stack(B[-1]))
#   a values ind
# 1 A      1   b
# 2 B      2   b
# 3 C      3   b
# 4 A      4   c
# 5 B      5   c
# 6 C      6   c

或者,您可以row.names = NULLcbind语句中设置:

cbind(A[1], stack(A[-1]), row.names = NULL)
#   a values ind
# 1 A      1   b
# 2 B      2   b
# 3 C      3   b
# 4 A      4   c
# 5 B      5   c
# 6 C      6   c

如果您的原件row.names很重要,您还可以将它们重新添加:

cbind(rn = rownames(A), A[1], stack(A[-1]), row.names = NULL)
#   rn a values ind
# 1  A A      1   b
# 2  B B      2   b
# 3  C C      3   b
# 4  A A      4   c
# 5  B B      5   c
# 6  C C      6   c
于 2014-05-08T06:43:40.620 回答