Items inside lists.

I am relatively new to R (~ 3 months) and therefore I just use all data types. While lists are a very useful way to store heterogeneous data in one place, they are also extremely inflexible for function calls, and they make me sad.

For the work that I do, I often use lists because I need to keep a bunch of vectors of different lengths. For example, I track performance statistics for about 10,000 different vehicles, and there are certain vehicles that are so similar that they can essentially be considered the same vehicles for certain analyzes.

So, let's say we have this list of car identifiers:

List <- list(a=1, b=c(2,3,4), c=5) 

For simplicity.

I want to do two things:

  • Tell me which list item has a particular car. Therefore, when I say R, I work with vehicle 2 , it should tell me b or [2] . I feel it should be something simple, how can you do

     match(3,b) > 2 
  • Convert it to a data frame or something similar so that it can be saved as a CSV. Unused strings can be empty or NA . What I should have done so far:

     for(i in length(List)) { length(List[[i]]) <- max(as.numeric(as.matrix(summary(List)[,1]))) } DF <- as.data.frame(List) 

Which seems silly.

+4
source share
2 answers

For your first question:

 which(sapply(List, `%in%`, x = 3)) # b # 2 

For your second question, you can use a function like this:

 list.to.df <- function(arg.list) { max.len <- max(sapply(arg.list, length)) arg.list <- lapply(arg.list, `length<-`, max.len) as.data.frame(arg.list) } list.to.df(List) # abc # 1 1 2 5 # 2 NA 3 NA # 3 NA 4 NA 
+4
source

Both of these tasks (and many others) would be much simpler if you "smoothed" your data in data.frame. Here is one way to do this:

 fun <- function(X) data.frame(element = X, vehicle = List[[X]], stringsAsFactors = FALSE) df <- do.call(rbind, lapply(names(List), fun)) # element vehicle # 1 a 1 # 2 b 2 # 3 b 3 # 4 b 4 # 5 c 5 

Using data.frame in hand, as you could do two tasks:

 ## Task #1 with(df, element[match(3, vehicle)]) # [1] "b" ## Task #2 write.csv(df, file = "outfile.csv") 
+4
source

Source: https://habr.com/ru/post/1441680/


All Articles