sábado, 4 de abril de 2020

R en español: Concatenar filas o columnas de distintos tamaños

Cuando se desea concatenar las filas o columnas de dos dataframes de distintos tamaños (length) se suele obtener este error:

#para filas
Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

#para columnas
Error in cbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

En estos casos se puede utilizar la función rbind.fill para filas o cbind.fill para columnas.

Las funciones rbind.fill y cbind.fill se encuentran en la librería rowr v1.1.3.

Ejemplo usando el database mtcars:

test <- mtcars[1:2,]
ncol(test)
# [1] 11

modified <- test[,1:9]
ncol(modified)
# [1] 9

rbind(test, modified)
# Error in rbind(deparse.level, ...) : 
  # numbers of columns of arguments do not match

library(plyr)
rbind.fill(test, modified)
  # mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1  21   6  160 110  3.9 2.620 16.46  0  1    4    4
# 2  21   6  160 110  3.9 2.875 17.02  0  1    4    4
# 3  21   6  160 110  3.9 2.620 16.46  0  1   NA   NA
# 4  21   6  160 110  3.9 2.875 17.02  0  1   NA   NA
Más información en el post original: https://stackoverflow.com/questions/46263982/error-in-rbinddeparse-level-invalid-list-argument-all-variables-should

No hay comentarios:

Publicar un comentario

R en Español: Obtener nombres de renglones con funcion row.names

Si deseo obtener los nombres de los renglones de mi dataframe puedo utilizar la función row.names de la paquetería básica de R y RStudio. ...