rbind

Split different lengths values and bind to columns

和自甴很熟 提交于 2019-11-26 21:26:35
问题 I've got a rather large (around 100k observations) data set, similar to this: data <- data.frame( ID = seq(1, 5, 1), Values = c("1,2,3", "4", " ", "4,1,6,5,1,1,6", "0,0"), stringsAsFactors=F) data ID Values 1 1 1,2,3 2 2 4 3 3 4 4 4,1,6,5,1,1,6 5 5 0,0 I want to split the Values column by "," with NA for missed cells: ID v1 v2 v3 v4 v5 v6 v7 1 1 2 3 NA NA NA NA 2 4 NA NA NA NA NA NA 3 NA NA NA NA NA NA NA 4 4 1 6 5 1 1 6 5 0 0 NA NA NA NA NA ... Best attempt was strsplit + rbind : df <- data

Converting nested list to dataframe

时间秒杀一切 提交于 2019-11-26 20:17:34
The goal is to convert a nested list which sometimes contain missing records into a data frame. An example of the structure when there are missing records is: str(mylist) List of 3 $ :List of 7 ..$ Hit : chr "True" ..$ Project: chr "Blue" ..$ Year : chr "2011" ..$ Rating : chr "4" ..$ Launch : chr "26 Jan 2012" ..$ ID : chr "19" ..$ Dept : chr "1, 2, 4" $ :List of 2 ..$ Hit : chr "False" ..$ Error: chr "Record not found" $ :List of 7 ..$ Hit : chr "True" ..$ Project: chr "Green" ..$ Year : chr "2004" ..$ Rating : chr "8" ..$ Launch : chr "29 Feb 2004" ..$ ID : chr "183" ..$ Dept : chr "6, 8"

How can I rbind vectors matching their column names?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 16:41:08
问题 rbind does not check for column names when binding together vectors: l = list(row1 = c(10, 20), row2 = c(20, 10)) names(l$row1) = c("A", "B") names(l$row2) = c("B", "A") l $row1 A B 10 20 $row2 B A 20 10 rbind(l$row1, l$row2) A B [1,] 10 20 [2,] 20 10 How can I produce this matrix from a number of list elements, insuring the column names are correctly matched across rows: A B [1,] 10 20 [2,] 10 20 回答1: You can use match : l <- list(row1 = setNames(1:3, c("A", "B", "C")), row2 = setNames(1:3,

Efficient way to rbind data.frames with different columns

核能气质少年 提交于 2019-11-26 11:52:52
I have a list of data frames with different sets of columns. I would like to combine them by rows into one data frame. I use plyr::rbind.fill to do that. I am looking for something that would do this more efficiently, but is similar to the answer given here require(plyr) set.seed(45) sample.fun <- function() { nam <- sample(LETTERS, sample(5:15)) val <- data.frame(matrix(sample(letters, length(nam)*10,replace=TRUE),nrow=10)) setNames(val, nam) } ll <- replicate(1e4, sample.fun()) rbind.fill(ll) Arun UPDATE: See this updated answer instead. UPDATE (eddi): This has now been implemented in

Why is rbindlist “better” than rbind?

百般思念 提交于 2019-11-26 11:03:17
I am going through documentation of data.table and also noticed from some of the conversations over here on SO that rbindlist is supposed to be better than rbind . I would like to know why is rbindlist better than rbind and in which scenarios rbindlist really excels over rbind ? Is there any advantage in terms of memory utilization? mnel rbindlist is an optimized version of do.call(rbind, list(...)) , which is known for being slow when using rbind.data.frame Where does it really excel Some questions that show where rbindlist shines are Fast vectorized merge of list of data.frames by row

R - use rbind on multiple variables with similar names

给你一囗甜甜゛ 提交于 2019-11-26 08:28:28
问题 I have many variables that I have created using code like this: for (i in 1:10) { assign(paste0(\"variable\", i), i )} I now need to use rbind on the variables to combine them. I tried something like this to no avail: rbind(assign(paste0(\"variable\", 1:10))) Any suggestions on what to do? 回答1: That is the wrong way to handle related items. Better to use a list or dataframe, but you will probably find out why in due course. For now: do.matrix <- do.call(rbind, lapply( ls(patt="variable"), get

Converting nested list to dataframe

允我心安 提交于 2019-11-26 07:33:46
问题 The goal is to convert a nested list which sometimes contain missing records into a data frame. An example of the structure when there are missing records is: str(mylist) List of 3 $ :List of 7 ..$ Hit : chr \"True\" ..$ Project: chr \"Blue\" ..$ Year : chr \"2011\" ..$ Rating : chr \"4\" ..$ Launch : chr \"26 Jan 2012\" ..$ ID : chr \"19\" ..$ Dept : chr \"1, 2, 4\" $ :List of 2 ..$ Hit : chr \"False\" ..$ Error: chr \"Record not found\" $ :List of 7 ..$ Hit : chr \"True\" ..$ Project: chr \

What&#39;s wrong with my function to load multiple .csv files into single dataframe in R using rbind?

青春壹個敷衍的年華 提交于 2019-11-26 03:56:37
问题 I have written the following function to combine 300 .csv files. My directory name is \"specdata\". I have done the following steps for execution, x <- function(directory) { dir <- directory data_dir <- paste(getwd(),dir,sep = \"/\") files <- list.files(data_dir,pattern = \'\\\\.csv\') tables <- lapply(paste(data_dir,files,sep = \"/\"), read.csv, header = TRUE) pollutantmean <- do.call(rbind , tables) } # Step 2: call the function x(\"specdata\") # Step 3: inspect results head(pollutantmean)