sto cercando di indovinare il vostro data.frame
ha 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
Quello che sta succedendo qui è che dal momento che non si può avere di default duplicato row.names
in un data.frame
e dal momento che non si dica a R qualsiasi punto per duplicare lo row.names
quando si ricicla la prima colonna sullo stesso numero di righe della colonna impilata, R scarta semplicemente lo row.names
.
Confronta con un simile data.frame
, ma uno senza 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
In alternativa, è possibile impostare row.names = NULL
nel cbind
dichiarazione:
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
Se l'originale row.names
sono importanti, c e aggiungili nuovamente con:
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
"Il codice funziona ancora come dovrebbe essere"; puoi usare try() per sopprimere l'avviso: dateset = try (cbind (dateet [c (1,2,3,4,5)], stack (dateet [, - c (1,2,3,4,5)])), silent = TRUE) – FFI