I have two data frame, this is just a sample , database have approx 1 million of records.
can have name, email, alphanumeric code etc.
data1<-data.frame(
'ID 1' = c(86364,"ARV_2612","AGH_2212","IND_2622","CHG_2622"),
sector = c(3,3,1,2,5),
name=c("nhug","hugy","mjuk","ghtr","kuld"),
'Enternal code'=c(1,1,1,1,3),
col3=c(1,1,0,0,0),
col4=c(1,0,0,0,0),
col5=c(1,0,1,1,1)
)
data2<-data.frame(
'ID 1' = c(53265,"ARV_7362",76354,"IND_2622","CHG_9762"),
sector = c(3,3,1,2,5),
name=c("nhug","hugy","mjuk","ghtr","kuld"),
'Enternal code'=c(1,1,1,1,3),
col3=c(1,1,0,0,0),
col4=c(1,0,0,0,0),
col5=c(1,0,1,1,1)
)
data2 %>% mutate(
duplicated = factor(if_else(`ID 1` %in%
pull(data1, `ID 1`),
1,
0)))
now i am looking for a function to mutate my one data frame (data2) like. if I give column names of data1 and data2 to find if the values or string already exist in other data and mutate a new column to 1,0 for true and false.
the function would be like
func(data1 = "name",data2="name",mutated_com="name_exist")
In base R, you can write this function as :
func <- function(data1, data2, data1col, data2col, newcol) {
data2[[newcol]] <- factor(as.integer(data2[[data2col]] %in% data1[[data1col]]))
data2
}
and can call it as :
func(data1, data2, 'name', 'name', 'duplicate')
This will create a column named duplicate in data2 giving 1 where the name in df2 is also present in name of df1 and 0 otherwise.
Using dplyr syntax the above can be written as :
library(dplyr)
library(rlang)
func <- function(data1, data2, data1col, data2col, newcol) {
data2 %>%
mutate(!!newcol := factor(as.integer(.data[[data2col]] %in%
data1[[data1col]])))
}
You can use an inner_join (from dplyr) to determine the overlap between the two dataframes. To use all columns (if both dataframes have the same column names) you do not have to specify the 'by' argument.
You can then add a column 'duplicated' and join back to the original dataframe (df1 or df2) to get the desired result.
overlap <- data1 %>%
inner_join(data2) %>%
mutate(duplicated = 1)
data1 %>% #or data2
left_join(overlap) %>%
mutate(duplicated = ifelse(is.na(duplicated),0,1))
I want to calculate the mean of each row in the 'value' column across a list of dataframes.
The output I want is a dataframe with the 'sample' name and its associated mean 'value' for each row across the whole list.
Below is some example data:
list <- list()
dataframe1 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
gr = c("1","1","2","5","4","5","5","3","2","2","2","4","3","1","1","3","2","1","2","5","5","5","2","2","2","1","2","1","1","1","2","1","1","2","2","5","3","3","5"),
value = c("14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500","14.32500"))
list[[1]] <- dataframe1
dataframe2 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
gr = c("5","4","3","5","4","5","5","3","2","2","2","2","3","1","1","3","2","1","2","5","5","5","2","2","2","1","2","1","1","1","2","1","1","2","4","4","4","4","4"),
value = c("12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000","12.59000"))
list[[2]] <- dataframe2
dataframe3 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
gr = c("5","3","3","5","5","5","5","3","5","3","3","3","3","3","3","3","2","1","2","1","1","1","2","2","2","1","2","1","1","1","2","1","1","4","4","4","4","4","4"),
value = c("20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915","20.06915"))
list[[3]] <- dataframe3
dataframe4 <- data.frame(sample = c("OP2645ii_c","OP5048___e","OP5048___f","OP5046___d","OP2645ii_e","OP2645ii_a","OP5054DNAa","OP5048___c","OP2645ii_d","OP5048___b","OP5047___a","OP5048___h","OP5053DNAb","OP3088i__a","OP5048___g","OP5053DNAa","OP5049___a","OP2645ii_b","OP5046___c","OP5044___c","OP2413iiia","OP5054DNAc","OP5046___e","OP5054DNAb","OP5044___a","OP5046___a","OP5046___b","OP2413iiib","OP5051DNAa","OP5048___d","OP5044___b","OP5049___b","OP5051DNAc","OP5051DNAb","OP5053DNAc","OP5047___b","OP5043___b","OP5043___a","OP5052DNAa"),
gr = c("2","2","2","3","4","5","5","3","2","2","2","4","5","1","1","3","2","1","2","5","5","5","2","2","2","1","2","1","1","1","2","1","1","2","2","5","3","3","5"),
value = c("18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500","18.32500"))
list[[4]] <- dataframe4
Many thanks.
Cheers.
Deon
Using base functions, you could extract all the value columns into a matrix and use row means:
rowMeans(sapply(list, "[[", "value"))
For you sample data, you'd need to also convert to numeric (as below), but I'm hoping your real data has numbers not factors.
rowMeans(sapply(lapply(list, "[[", "value"), function(x) as.numeric(as.character(x))))
This just gives the values (and assumes the rows are in the right order). You can add the sample names with cbind, e.g., cbind(list[[1]][["sample"]], rowMeans(...)).
We can bind the list elements to a single data.frame, create a group of row_number() and use that to get the mean of 'value'
library(dplyr)
bind_rows(list, .id = 'id') %>%
mutate(value = as.numeric(value)) %>%
group_by(id) %>%
group_by(grp = row_number(), sample) %>%
summarise(value = mean(value, na.rm = TRUE))