简体   繁体   中英

Removing values, stored in character vector, from a list

I want to remove certain values from a list to create a refined list. I have all of the values I want to remove stored in a character vector named remove. The values in remove correspond to the first column of the list. I've run the following code:

refined_list = list
for (i in length(list)){
  if (refined_list[i,1] %in% remove){
  refined_list = refined_list[-i,]
}
  else{
    refined_list = refined_list
}
}

only the initialization of refined list seems to register. No errors, but refined_list is identical to list. It's a mystery to me

It doesn't seem like you're actually talking about lists, since a list cannot be subset as you are proposing (ie list[, 1] ). But if you're looking for a solution for a data.frame , here's one:

# Set up some test data
dd <- data.frame(letters = letters[1:10], stringsAsFactors = FALSE)
remove <- letters[c(1, 4, 6)]

# Shed values that are in remove
dd[!(dd[, 1] %in% remove), 1, drop = FALSE]

#> "b" "c" "e" "g" "h" "i" "j"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM