cbind警告:从短变量中找到了行名,并且已将其丢弃 [英] cbind warnings : row names were found from a short variable and have been discarded
问题描述
我在cbind的代码行下面,但是每次都收到警告消息。
尽管代码仍然可以正常运行,但是有什么方法可以解决警告?
I have below line of code for cbind, but I am getting a warning message everytime. Though the code still functions as it should be, is there any way to resolve the warning?
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
谢谢!
推荐答案
我猜你的数据了。框架
具有 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
/ code>,因为在将第一列回收到堆叠的相同行数时,您在任何时候都不告诉R复制 row.names
列,R只是丢弃 row.names
。
What's happening here is that since you can't by default have duplicated row.names
in a data.frame
and since you don't tell R at any point to duplicate the row.names
when recycling the first column to the same number of rows of the stacked column, R just discards the row.names
.
与相似的数据进行比较。框架
,但没有 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
或者,您可以在 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
如果原始行.names
很重要,您也可以使用以下方式将它们重新添加:
If your original row.names
are important, you can also add them back in with:
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
这篇关于cbind警告:从短变量中找到了行名,并且已将其丢弃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!