rbind

Performance of rbind.data.frame

烂漫一生 提交于 2019-11-28 23:55:52
I have a list of dataframes for which I am certain that they all contain at least one row (in fact, some contain only one row, and others contain a given number of rows), and that they all have the same columns (names and types). In case it matters, I am also certain that there are no NA's anywhere in the rows. The situation can be simulated like this: #create one row onerowdfr<-do.call(data.frame, c(list(), rnorm(100) , lapply(sample(letters[1:2], 100, replace=TRUE), function(x){factor(x, levels=letters[1:2])}))) colnames(onerowdfr)<-c(paste("cnt", 1:100, sep=""), paste("cat", 1:100, sep=""))

R: losing column names when adding rows to an empty data frame

筅森魡賤 提交于 2019-11-28 05:37:39
I am just starting with R and encountered a strange behaviour: when inserting the first row in an empty data frame, the original column names get lost. example: a<-data.frame(one = numeric(0), two = numeric(0)) a #[1] one two #<0 rows> (or 0-length row.names) names(a) #[1] "one" "two" a<-rbind(a, c(5,6)) a # X5 X6 #1 5 6 names(a) #[1] "X5" "X6" As you can see, the column names one and two were replaced by X5 and X6 . Could somebody please tell me why this happens and is there a right way to do this without losing column names? A shotgun solution would be to save the names in an auxiliary

R 3.0.3 rbind multiple csv files

泄露秘密 提交于 2019-11-28 04:18:42
问题 R 3.0.3: I have 40 csv files all structured the same that I want to rbind into one file so I can calculate the mean of one column. I searched: this website R in a Nutshell R_Intro sources ?rbind Help in RStudio I cannot find the answer. Any suggestions/pointers? 回答1: Using the answer from here [Importing several files and indexing them ] list files with .csv extension - this assumes that the only .csv files in your working directory are the ones you want to read files <- list.files(pattern =

Create a variable that identifies the original data.frame after rbind command in R

这一生的挚爱 提交于 2019-11-28 03:59:40
问题 I am relatively new to R and I would like to know how can I create a variable (number sequence) that identifies the each of the original data.frames before being joined with the rbind command. Since in the original data frames there is one variable that is a row ID number, if creating a loop that assigns a new number in the new variable each time it encounters the number 1 in the row ID, it should work... Thanks. 回答1: There's a function in the gdata package called combine that does just that.

Merging df.1, df.2, df.3 … df.x into one dataframe

会有一股神秘感。 提交于 2019-11-28 01:46:14
I am working on a project that imports all csv files from a given folder and merges them into one file. I was able to import the rows and columns I wanted from each of the files from the folder but now need help merging them all into one file. I do not know how many files I will eventually end up with (probably around 120) so I do not want to merge them 1 by 1. Here is what I have so far: # Import All files rowsToUse <- c(9:104,657:752) colsToUse <- c(15,27,28,29,30,33,35) filenames <- list.files("save", pattern="*.csv", full.names=TRUE) for (i in seq_along(filenames)) { assign(paste("df", i,

How can I rbind vectors matching their column names?

寵の児 提交于 2019-11-27 14:18:30
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 You can use match : l <- list(row1 = setNames(1:3, c("A", "B", "C")), row2 = setNames(1:3, c("B", "C", "A")), row3 = setNames(1:3, c("C", "A", "B"))) do.call(rbind, lapply(l, function(x) x[match

How can I prevent rbind() from geting really slow as dataframe grows larger?

眉间皱痕 提交于 2019-11-27 09:07:00
I have a dataframe with only 1 row. To this I start to add rows by using rbind df #mydataframe with only one row for (i in 1:20000) { df<- rbind(df, newrow) } this gets very slow as i grows. Why is that? and how can I make this type of code faster? You are in the 2nd circle of hell , namely failing to pre-allocate data structures. Growing objects in this fashion is a Very Very Bad Thing in R. Either pre-allocate and insert: df <- data.frame(x = rep(NA,20000),y = rep(NA,20000)) or restructure your code to avoid this sort of incremental addition of rows. As discussed at the link I cite, the

How to rbind matrices based on objects names?

眉间皱痕 提交于 2019-11-27 07:07:13
问题 I have several matrices that I would like to rbind in a single summary one. They are objects product of different functions and they have share a pattern in their names. What I want to do is to tell R to look for all the objects with that common pattern and then rbind them. Assuming these matrices exist: commonname.N1<-matrix(nrow=2,ncol=3) commonname.N2<-matrix(nrow=2,ncol=3) commonname.M1<-matrix(nrow=2,ncol=3) I tried something like this to get them: mats<-grep(x= ls(pos=1), pattern=

Difference between rbind() and bind_rows() in R

好久不见. 提交于 2019-11-27 01:36:42
问题 On the web,i found that rbind() is used to combine two data frames and the same task is performed by bind_rows() function. Then i don't understand what is the difference between these two functions and which is more efficient to use ?? 回答1: Apart from few more differences, one of the main reasons for using bind_rows over rbind is to combine two data frames having different number of columns. rbind throws an error in such a case whereas bind_rows assigns " NA " to those rows of columns missing

R - use rbind on multiple variables with similar names

匆匆过客 提交于 2019-11-26 22:57:04
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? 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) ) Or: do.matrix <- do.call(rbind, lapply( paste0("variable", 1:10) , get) ) I would like to add another way to