2014-05-08 12 views
18

Ho sotto la riga di codice per cbind, ma ricevo sempre un messaggio di avviso. Sebbene il codice funzioni ancora come dovrebbe essere, esiste un modo per risolvere l'avviso?avvertimenti cbind: i nomi di riga sono stati trovati da una variabile breve e sono stati scartati

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)])) 

Avvertenze:

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

Grazie in anticipo!

+0

"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

risposta

30

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 
+3

Siamo spiacenti, Ananda. Non stavo cercando di sfidarti affatto. –

+2

@RichardScriven, non significava che sembrasse così e hai eliminato la tua risposta. Come ho indicato, 'unlist' * dovrebbe * avere un vantaggio sulle prestazioni (per non parlare del fatto che funzionerà con' factor's, che 'stack' non lo farà). Infatti, lo uso nella mia versione attuale della mia funzione ['Stacked'] (https://github.com/mrdwab/splitstackshape/blob/devel/R/Stacked.R#L76) (che era originariamente basata su' stack') e ho pensato di commentare con le osservazioni che ho trovato nel processo. – A5C1D2H2I1M1N2O1R2T1

+0

Grande. Ho avuto lo stesso messaggio di avviso con '' transform() '' e la stessa soluzione ha funzionato: '' transform (dati, ..., row.names = NULL) '' – PatrickT

Problemi correlati