我有下面的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
提前感谢!
发布于 2014-05-08 14:43:41
我猜你的data.frame
上有row.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
这里发生的情况是,由于默认情况下不能在data.frame
中复制row.names
,并且在将第一列回收到堆叠列的相同行数时,您在任何时候都不会告诉R复制row.names
,因此R只是丢弃了row.names
。
与不带row.names
的类似data.frame
进行比较
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
或者,您可以在cbind
语句中设置row.names = NULL
:
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
https://stackoverflow.com/questions/23534066
复制相似问题