i have a huge number of vectors in R, all with the same length, say: x1,x2,...,x1000000
and i want to cbind some of into one single dataframe. I have the list of the names of vectors that i want to combine, say name_list <- c("x3","x47847","x930233") of vectors x3,x47847,x930233
So normally we would just use cbind(x3,x47847,x930233). But since the number of vectors is huge, and all i have is the name list, is there any way i can just do the cbind with name_list?
Thank you so much for your help in advance.
Related
I want to produce a vector containing all k-element subsets of a second vector. I know that I can do this by applying vecextract with each k-element subset of the natural numbers 1...n to my original vector.
How can I create that vector of subsets of natural numbers, though? I can see that the command forsubset does nearly what I want, but it's an imperative command, not one which creates a vector. So I could use the following function to print a list of vectors:
f(n,k)=forsubset([n,k],s,print(s)) but I can only capture them by adding each subset to a List() and then converting the List() to a Vec(). This seems clumsy. Is there a better way to do this, perhaps a totally different one?
I am trying to remove the first 9 rows of multiple dataframes that have the same structures but different names (keeping similar name structure). In my example, there are 4 dataframes with respectively the names
Mydataframe_A, Mydataframe_B, Mydataframe_C, Mydataframe_D.
Currently it is working with the following code:
`Mydataframe_A`<- `Mydataframe_A`[-c(1:9),]
`Mydataframe_B`<- `Mydataframe_B`[-c(1:9),]
`Mydataframe_C`<- `Mydataframe_C`[-c(1:9),]
`Mydataframe_D`<- `Mydataframe_D`[-c(1:9),]
But I would like to write this is with only one line and not having to specify each time each name of dataframe.
I think this could work by using a pattern name and lists because for example this is what I am doing to rbind different dataframes:
All_mydataframes <- rbindlist(mget(ls(pattern = "^Mydataframe_")))
Any idea on how to do this ?
Thanks a ton!
Since mget turns this into a list, you can use apply family functions:
rbindlist(lapply(mget(ls(pattern = "^Mydataframe_")), function(x) x[-c(1:9), ]))
This takes the list from mget and removes the first 9 rows, then rbind it from list to data.table. The only problem is you can't differentiate what data.frame the original data was part of.
I want to extract multiple sublists of different size into different data.frames one dataframe for each sublist.
Does anyone know how I can do that?
I tried:
extract_one <- function(x) {
data.frame(dat[[1]]) }
out <- lapply(dat, extract_one).
My idea was to extract the first part of the list and then leveraging lapply to do it for every sublist.
Obviously does not work.
Any help would be highly appreciated!
Regards
I have to write an R script where I want to load different numbers of files at different times. The files are loaded into data frames and certain columns of the data frames are extracted. The columns are then merged with the cbind function. My problem is that I do not know how I can adapt to varying numbers of files that are loaded from time to time, because there may be 3 vectors for cbind at one time or 5 vectors at another time. So how can I give cbind a number of vectors so that it doesn't output errors when it doesn't get all vectors? This happens when I give it a fixed number.
raw1 <- read.table()
raw2 <- read.table()
vec1 <- raw1[,2]
vec2 <- raw2[,2]
cbind(vec1,vec2,vec3)
I know I'd better write sth interactive such as a tcltk dialog and the some kind of loop. Maybe you could provide me with some kind of an idea of how an effective loop could be structured.
You can store the data frames in a list and then cbind them using do.call(). This is a good way to cbind lists of arbitrary length.
datalist <- lapply(filenames, function(i) read.table(i)[, 2])
# ... where filenames are the names of the files you want to read, and
# passing any additional parameters to read.table that are needed
# Then cbind all the entries of datalist
do.call(cbind, datalist)
I am dealing with about 10 data frames that have the same column names, but different number of rows. I would like to create a list of all columns with the same names.
So, say i have 2 data frames with the same names.
a<-seq(0,20,1)
b<-seq(20,40,1)
c<-seq(10,30,1)
df.abc.1<-data.frame(a,b,c)
a<-seq(20,50,1)
b<-seq(10,40,1)
c<-seq(30,60,1)
df.abc.2<-data.frame(a,b,c)
I know i can create a list from this data such as,
list(df.abc.1$a, df.abc.2$a)
but i don't want to type out my long data frame names and column names.
I was hoping to do something like this,
list(c(df.abc.1, df.abc.2)$a)
But, it returns a list of df.abc.1$a
Perhaps there could be a way to use the grep function across multiple data.frames?
Perhaps a loop could accomplish this task?
Not sure if it's any better, but maybe
lapply(list(df.abc.1, df.abc.2), function(x) x$a)
For more than one column
lapply(list(df.abc.1, df.abc.2), function(x) x[, c("a","b")])