简体   繁体   中英

R - number of columns of matrices must match in rbind

I am trying to rbind the list of matrices to create a dataframe but getting error.

txt<-grep("^#",
       readLines("offline.txt"),
       invert = TRUE,
       value = TRUE)


processLine = function(x)

{

 tokens = strsplit(x, "[;=,]")[[1]]
 tmp = matrix(tokens[ - (1:10) ], ncol = length(tokens[-(1:10)]), byrow = TRUE)
 cbind(matrix(tokens[c(2, 4, 6:8, 10)], nrow = nrow(tmp), ncol = 6, byrow= TRUE), tmp)

 }

 tmp = lapply(txt, processLine)

 sapply(tmp, nrow)

 offline = as.data.frame(do.call("rbind", tmp))


 Error: Error in rbind(c("1139643118358", "00:02:2D:21:0F:33", "0.0", "0.0", "0.0", : number of 
 columns of matrices must match (see arg 2)

Link of dataset Dataset

Try the data.table library.

library(data.table)
df<-rbindlist(tmp, fill = T)

Make sure the items in the list are in fact data.frame, data.table or list. Not a matrix or array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM