How to multiply columns of same names belonging to different data.frame - r

I am having a problem... I have two data. frames with a lot of columns and these two data.frames are of different length, in fact one has many rows and second data.frame has only one row.... But in both data frames there are columns of same names. Now, I want to multiply the matching columns with each other. I fail to solve it. Please help me.

The command
mapply("*", DataFrame1, DataFrame2)
should work if you want to multiply all columns. If the relevant columns are only a subset of all columns in the data frames, we first need to identify the columns being present in both data frames.
mapply("*", DataFrame1[intersect(names(DataFrame1), names(DataFrame2))],
DataFrame2[intersect(names(DataFrame1), names(DataFrame2))])

Related

Merging data frames into another dataframe

I'm working with R statistics. I'm trying to make a data frame that merges other three data frames. Those three data frames have different column names & different row numbers (they don't have row names).
I tried originally to do:
Namenewdf <- data.frame(dataframe1, dataframe2, dataframe3)
R marked an error because of differing number of rows.
Then I tried with the merge function but it also didn't work.
How do I merge the data frames so that the resulting data frames include the original information of the data frames used as arguments, not filling the 'void' rows from the data frames that have fewer rows?
library(rowr)
finaldataframe<-cbind.fill(dataframe1,dataframe2, dataframe3,fill = NA)
finaldataframe[is.na(finaldataframe)]<-""

Extract the last column from a list of data frames

I have a list with ten data frames with different number of columns and I would like to get a list with ten vectors. Each vector would be the last column from each data frame.
If I want to get the last column from the first data frame, I run:
lapply(list_with_df,function(x) x[,lengths(list_with_df)[1]])
But I have got the same column number for each data frame.
I have tried to do a "loop for" through the ten data frames, but I have got an error. I appreciate if someone could help me with this matter. Regards.
Instead of using the lengths you can ask of the number of columns by ncol. This should work:
lapply(list_with_df,function(x) x[,ncol(x)])
Edit
Just for some clarification: The reason why you got the same column number for each data frame is because you have always selected the column number according to the first element of lengths vector by using lengths(list_with_df)[1]. It was always the length of the first data.frame

Remove part of name ending in dataframe column named .id

Background
I have some big dataframes (ie 15000 obs. of 100 variables) in which one similarity is that one of the columns is named .id.
I need to prepare the big dataframes for merging with each other. In order to perform the merging, then the columns named .id needs to have the same values.
All the dataframes columns named .id have the same beginning of random values call it randomValues, but there is two different type of endings call them randomValues-ending_1 and randomValues-ending_2.
The question
How does one remove remove the -ending_1 and -ending_2 text from the .id column of these big dataframe?
Any help is much appreciated :)
colnames(big.dataframe) <- gsub("-ending_\d+$","",colnames(big.dataframe))

R: multiple merge with big data frames

I have two big dataframes: DBa and DBb. All colums of DBb are in DBa.
I want to merge these two dataframes by all DBb's colums.
I'm trying:
new <- merge(DBa, DBb, by=colnames(DBb))
but it gives me the error:
Elements listed in `by` must be valid column names in x and y
How can I do it?
I don't think you are looking to merge the data frames, you should put them on top of each other with rbind. With merge you will put two data frames next to eachother, and you only need one common column (the key) which should be unique otherwise the results will be a mess.
So use row bind (rbind). The columns must be in the same order and one data frame must not have more columns than the other.
new_data <- rbind(data1, data2)

Extract columns with same names from multiple data frames [R]

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")])

Resources