This question already has answers here:
R - use rbind on multiple variables with similar names
(2 answers)
rbind multiple data sets [duplicate]
(4 answers)
Closed 3 years ago.
I have some data frame data1, data2, data10.
I want to combine all those data frames by rows.
I tried using rbind in a loop but i can't call dataframe correctly in the loop.
tempo<-data.frame()
for(j in 1:10){
tempo<-rbind(tempo,eval(parse(paste0("data",j))))
}
but that doesn't work. R doesn't know the dataframe that i want to call with command eval(parse)
Thank you
Related
This question already has answers here:
Combine several data frames in the global environment by row (rbind)
(2 answers)
How to rbind all the data.frames in your working environment?
(1 answer)
Closed 2 years ago.
I have 5 dataframes all with the same heading and columns
df1,df2,df3,df4,df5
I created a list based on the dataframe names
List<-c("df1","df2","df3","df4",d"f5")
I want to create an rbind looping the df's based off the list. i tried this
ALL_dfs<-lapply(List,rbind)
but this doesn't read it as a combined dataframe by rbind function.
I think the most straightforward option is this:
do.call(rbind, mget(List))
?mget
Search by name for an object (get) or zero or more objects (mget).
The other option, if possible, is to store your data.frames in a list when you create them. Not always an option, but may be possible with some planning
With map_df from purrr.
library(purrr)
all_dfs <- map_dfr(.x = List, .f = bindr)
This question already has answers here:
number of rows each data frame in a list [duplicate]
(2 answers)
Closed 3 years ago.
I have 10 lists stored in the object squared.res$squared that can be called using squared.res$squared[1] (list1) squared.res$squared[2] list2 etc. I would like to get the number of rows for each list in the object.
I tried to convert to data frame and do a loop, but the output is empty. I did not manage to produce a data example.
EDIT: squared.res is a list
for (i in 1:10) {
dim(do.call(rbind.data.frame, squared.res$squared[i]))[1]
}
If squared.res$squared is a list, it seems that
lapply(squared.res$squared, length)
should work. Or with sapply instead of lapply, or dim instead of length as #jogo proposes.
This question already has an answer here:
Access variable dataframe in R loop
(1 answer)
Closed 6 years ago.
I want to access data frames in a loop(df_1,df_2,df_3,df_4).So I tried to use the following code
for(i in 1:4){
total[i]=sum(paste0("df_",i)$percentage)
}
But it throws the error operator invalid for atomic vector.So how is it possible to read the data frames in a loop?
What you probably are trying to do is,
listdf <- mget(paste0("df", 1:4))
sapply(listdf, function(x) sum(x["percentage"]))
accessing the object with name using mget and making a list of dataframes and then for each dataframe in the list finding the sum of the percentage column
This question already has answers here:
Split data.frame based on levels of a factor into new data.frames
(3 answers)
Closed 8 years ago.
I have this data.frame:
df = data.frame(chr=c("chr1","chr1","chr1"),start=c(1,2,3),end=c(11,12,13),strand=c("+","+","-"),tid=c("t1","t1","t2"),exon_id=c(1,2,1))
I'm looking for a function that would convert df to a list of data.frames where the aggregating value is df$tid.
Without a function, for this toy example, it would be:
df.list = list(df[1:2,], df[3,])
Since df$tid[1:2] is t1.
You are looking for split:
split(df, df$tid)
should do the trick.
This question already has answers here:
How to count rows?
(3 answers)
Closed 5 years ago.
I have a list of data frames in R. They are created using lapply and read.csv commands. I want to know the number of rows of each data frame in the list. The nrow command does not work for this list. I am not sure may because it is a list. Any idea about counting the number of rows of each data frame in a list?
Lists don't have rows. Use sapply to loop over the list:
sapply(data.frame.list, nrow)
or lapply- the difference is in the output
lapply(data.frame.list, function(x) nrow(x))