Home > Net >  Pick the corresponding row position and name
Pick the corresponding row position and name

Time:06-08

I have a list of values and their corresponding row position provided as id. Essentially, given a vector of names I want to grab the row position from the list and assign the name it's from as a column. I can achieve the first part, but I cannot assign the names accordingly.

For example

predictors <- c('status', 'verbal')
  row_predictor_value <-
    lapply(row_predictor_data, function(x)
      which(x$name %in% predictors, arr.ind = TRUE) %>% setNames(., predictors))

Produces the following result:

[[1]]
status verbal 
     2      4 

[[2]]
status verbal 
     2      4 

[[3]]
status verbal 
     2      4 

However, this assigns the wrong name from where I got it.

It should produce instead:

[[1]]
status verbal 
     2      4 

[[2]]
verbal status 
     2      4 

[[3]]
status verbal 
     2      4 

Here's some example data:

row_predictor_data <- list(structure(list(id = structure(1:4, .Label = c("1", "2", 
"3", "4"), class = "factor"), name = structure(c(1L, 3L, 2L, 
4L), .Label = c("(Intercept)", "income", "status", "verbal"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(id = structure(1:4, .Label = c("1", "2", 
"3", "4"), class = "factor"), name = structure(c(1L, 4L, 2L, 
3L), .Label = c("(Intercept)", "sex", "status", "verbal"), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L)), structure(list(id = structure(1:5, .Label = c("1", "2", 
"3", "4", "5"), class = "factor"), name = structure(c(1L, 4L, 
2L, 5L, 3L), .Label = c("(Intercept)", "income", "sex", "status", 
"verbal"), class = "factor")), class = "data.frame", row.names = c(NA, 
-5L)))

CodePudding user response:

The issue with which and %in% is that it returns the positions without differentiating the order of 'predictors', thus if the order is different as in the second list element, when we use a fixed 'predictors' to assign as names, this gets the wrong result. Instead, use the 'names' column to generate the names of the vector

lapply(row_predictor_data, function(x) 
   with(subset(x, name %in% predictors),
      setNames(as.character(id), name)))

NOTE: Here we return a named vector of 'id's

  •  Tags:  
  • r
  • Related