R - Upsert on a dataframe -


i working in r. have 4 data frames data attempting summarize new dataframe. 4 starting frames have rownames unique identifiers (the rest have data identifier). there potential overlap, i.e. id might show in more 1 of 4 tables.

i attempting build dataframe following format:

id-dataset1-dataset2-dataset3-dataset4 "1"-false-false-true-true 

basically says id 1 appeared in datasets 3 , 4. goal come boolean vector each id, tells datasets found in. have 4 datasets dataframes, , rownames ids. since building final dataframe (calling vectortable) iteratively, initialize empty data frame. have started working on function folowing:

  1. check if id in vector table
    • if update correct boolean value
  2. otherwise build new boolean vector , add it

here code function:

mapidtovector <- function(id, vectortable, dataidx) {      if(id %in% vectortable$id) {         vectortable[test$id == id][dataidx] = true     } else {          # create vector row         row <- c(id, false, false, false, false)         row[idx] = true          rbind(vectortable, row)     } } 

here attempt @ getting work, starting 1 dataset table.

idvectorization <- data.frame(id=character(), ds1=logical(), ds2=logical(), ds3=logical(), ds4=logical())  # 2 ds1 since there id column lapply(row.names(ds1), mapidtovector, idvectorization, 2) 

the issue vectortable not getting updated. don't know if issue rbind() or pass reference / value. tips on how work appreciated!

here's how approach it.

as didn't provide data, here's some:

a <- data.frame(id = letters[1:3]) b <- data.frame(id = letters[3:6]) d <- data.frame(id = letters[6:9]) e <- data.frame(id = letters[9:12]) 

first, i'll combine dataframes list , out ids:

datlist <- list(a,b,d,e) allids <- unique(unlist(sapply(datlist, function(x) as.character(x[["id"]])))) 

then each id, check each data frame, using nested sapply:

t(sapply(allids, function(x) sapply(datlist, function(y) x %in% y[["id"]])))     [,1]  [,2]  [,3]  [,4]  true false false false b  true false false false c  true  true false false d false  true false false e false  true false false f false  true  true false g false false  true false h false false  true false false false  true  true j false false false  true k false false false  true l false false false  true 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -