我有下面的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)]))
Run Code Online (Sandbox Code Playgroud)
警告:
Warning message:
In data.frame(..., check.names = FALSE) :
row names were found from a short variable and have been discarded
Run Code Online (Sandbox Code Playgroud)
提前致谢!
A5C*_*2T1 43
我猜你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
Run Code Online (Sandbox Code Playgroud)
这里发生的是因为你不能默认row.names在a中复制data.frame,因为你没有告诉R在任何时候重复row.names将第一列重新循环到堆叠列的相同行数时,R只是丢弃了row.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
Run Code Online (Sandbox Code Playgroud)
或者,您可以row.names = NULL在cbind声明中设置:
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
Run Code Online (Sandbox Code Playgroud)
如果您的原件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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18323 次 |
| 最近记录: |