I've got two data tables with the same structure and I'd like to combine them into one.
I've read about the merge function, but as I've understood, that'd try to merge by common values, so I don't know if that's what I'm looking for.
It'd be something like this:
You can use the rbind() function.
new_data <- rbind(data1, data2)
Hope this helps.
Related
I want to create several data frames from an original one in R using for loop.
I want to get three separated data frames for each species to conduct separate analysis.
I tried the following code but it doesn't work:
data(iris)
library(dplyr)
for i in levels(iris$Species){
paste0(i,".data") <- data.frame(filter(iris,
Species=="i"))
}
I do not necessarily need dplyr but it's the one I am used to.
I think it is better to keep the separate frames in a list, which as pointed out in the comments is done easily with split(iris, iris$Species)
If you really want them out of the list and into separate named frames, you can use
list2env(split(iris, iris$Species),envir = .GlobalEnv)
An even better approach would be to keep everything in one data frame and then use dplyr/tidyr’s group_by() and nest() functions to fit a model for each group.
See here for a detailed walkthrough: https://tidyr.tidyverse.org/articles/nest.html
I think this one is easy but I still can't figure it out and I really need help with this. I've looked everywhere but still couldn't find it.
Let's say I have this vector:
filenames <- c("fn1", "fn2", "fn3")
And I want to associate them with an dataframe that is created according to a function, that is generated at that time
df|name from filenames[i]| <- df
so it would return these dataframes
dffn1
dffn2
dffn3
I hope I made myself clear. My problem is create a new data frame and name it according to a list or whatever, in a for loop.
You can use assign to achieve what you want.
for(nms in filenames){
assign(paste('df',nms,sep=''), df) }
I have this example data
list_1<-list(data.frame(c(1:10)),data.frame(c(11:20)))
list_2<-list(data.frame(c(21:30)),data.frame(c(31:40)))
And I need to join them together to get structure like
list_3<-list(data.frame(c(1:10)),data.frame(c(11:20)),data.frame(c(21:30)),data.frame(c(31:40)))
It means that I have to create one new list of frames. Because when I use
list_3<-list(list_1,list_2)
then the first frame in list_1 is list_3[[1]][[1]] and it is problem for me. I need to call this frame like list_3[[1]].
Any straightforward way how to achieve it?
I have tried some plyr like join, join_all and I cannot still done this.
Moving some comments to the correct place (answers), the two most common solutions would be:
c(list_1, list_2)
or
append(list_1, list_2)
Since you had already tried:
list(list_1, list_2)
and found that this had created a nested list, you can also unlist the nested list with the argument recursive = FALSE.
unlist(list(list_1, list_2), recursive = FALSE)
I am trying to go from a data frame to a list structure in R (and I know technically a data frame is a list). I have a data frame containing reference chemicals and their mechanisms different targets. For example, estrogen is an estrogen receptor agonist. What I would like is to transform the data frame to a list, because I am tired of typing out something like:
refchem$chemical_id[refchem$target=="AR" & refchem$mechanism=="Agonist"]
every time I need to access the list of specific reference chemicals. I would much rather access the chemicals by:
refchem$AR$Agonist
I am looking for a general answer, even though I have given a simplified example, because not all targets have all mechanisms.
This is really easy to accomplish with a loop:
example <- data.frame(target=rep(c("t1","t2","t3"),each=20),
mechan=rep(c("m1","m2"),each=10,3),
chems=paste0("chem",1:60))
oneoption <- list()
for(target in unique(example$target)){
oneoption[[target]] <- list()
for(mech in unique(example$mechan)){
oneoption[[target]][[mech]] <- as.character(example$chems[ example$target==target & example$mechan==mech ])
}
}
I am just wondering if there is a more clever way to do it. I tried playing around with lapply and did not make any progress.
Using split:
split(refchem, list(refchem$target, refchem$mechanism))
Should do the trick.
The new way to access would be refchem$AR.Agonist
If you make a keyed data.table instead, ...
you'll still have all the data in one data.frame (instead of a possibly-nested list of many);
you may find iterating over these subsets nicer; and
the syntax is pretty clean:
To access a subset:
DT[.('AR','Agonist')]
To do something for each group, that will be rbinded together in the result:
DT[,{do stuff},by=key(DT)]
Similar to aggregate(), any list of vectors of the correct length can go into the by, not just the key.
Finally, DT came from...
require(data.table)
DT <- data.table(refchem,key=c('target','mechanism'))
You can also use a plyr function:
library(plyr)
dlply(example, .(target, mechan))
It has the added advantage of using a function to process the data, if needed (there's an implicit identity in the above).
I am producing a script where I have done many manipulations to a bunch of data and, I do these same manipulations to another dataset. Both data sets have the same rows, columns, and headers. I would like to be able to join the two data sets together where I place dataset A above dataset B. I wouldn't need to headers for dataset B and would instead just clump all of the data together as if they were never really separated in the first place. Is there a simply way to do this?
Yes. Use rbind() command.
combineddataset = rbind(dataset1, dataset2)
Hope that helps.
And for completeness, you could also use the rbind.fill function found in the plyr package.