Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

Searching and coding using a list in R

Searching and coding using a list in R


I have a 'list' of 'vectors' of strings and a 'data.frame' of strings as follows

lst <- list( c("key", "parking", "velvet"), c("sumatra", "cap"), c("sled", "card"), c("notice", "piece", "page"))    df <-  c("key", "sumatra", "band", "cattle", "camp", "sled", "page", "wire", "key", "card", "cap", "page")  df <- data.frame(df, stringsAsFactors=FALSE)  

I would like to add column to dataframe df with codes based on membership in vectors in list lst. The desired output is like this.

df$code <- c("G1", "G2", "", "", "", "G3", "G4", "", "G1", "G3", "G2", "G4")     df          df code  1      key   G1  2  sumatra   G2  3     band       4   cattle       5     camp       6     sled   G3  7     page   G4  8     wire       9      key   G1  10    card   G3  11     cap   G2  12    page   G4  

How can I do this in R?

Answer by John Paul for Searching and coding using a list in R


I am assuming that you start with the codes like this:

 MyCode <- c("G1", "G2","G3", "G4", "G1", "G3", "G2", "G4")  

but you need to know what rows to put them in. Try this:

df$code<-NA  df[df$df %in% unlist(lst),]$code<-MyCode  

The unlist() part will turn your lists into a vector. The %in% part will return any row where df$df matches something in lst. Where there is no match, there will be a NA under df$code.

Answer by Matthew Plourde for Searching and coding using a list in R


Here's one way:

names(lst) <- paste0('G', seq_along(lst))  transform(df, code=with(stack(lst), ind[match(df, values)]))  #         df code  # 1      key   G1  # 2  sumatra   G2  # 3     band   # 4   cattle   # 5     camp   # 6     sled   G3  # 7     page   G4  # 8     wire   # 9      key   G1  # 10    card   G3  # 11     cap   G2  # 12    page   G4  

Answer by Guest for Searching and coding using a list in R


df$code <- paste0("G",cumsum(c(TRUE, diff(sequence(sapply(lst,length)))<0)))[match(df$df, unlist(lst))]  df$code[is.na(df$code)] <- ''  

Answer by Tyler Rinker for Searching and coding using a list in R


Here's an approach using the qdapTools package:

library(qdapTools)  names(lst) <- paste0("G", 1:length(lst))  df$code <- df[, 1] %l% lst  

Answer by Richard Scriven for Searching and coding using a list in R


And one more for good measure...

lst <- list(c("key", "parking", "velvet"), c("sumatra", "cap"),               c("sled", "card"), c("notice", "piece", "page"))  d <- c("key", "sumatra", "band", "cattle", "camp",           "sled", "page", "wire", "key", "card", "cap", "page")  DF <- data.frame(d, stringsAsFactors=FALSE)  

> l <- rep(seq_along(lst), sapply(lst, length))  > m <- l[match(d, unlist(lst))]  > DF$code <- ifelse(is.na(m), "", paste0("G", m))  > DF  ##         df code  ## 1      key   G1  ## 2  sumatra   G2  ## 3     band       ## 4   cattle       ## 5     camp       ## 6     sled   G3  ## 7     page   G4  ## 8     wire       ## 9      key   G1  ## 10    card   G3  ## 11     cap   G2  ## 12    page   G4  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.