简体   繁体   中英

delete element from list with R

I have a list of words

 [[1]]    
  [1] Hello
  [2] all    
  [3] the    
  [4] world
 [[2]]
  [1] world 
  [2] my  
  [3] Hi

And a data frame which contains for each word its frequency in the text:

Var1    Freq 
Hello   54 
all     178 
the     53 
entire  57
world   49

I would like to know if is there any way to remove each string of the data frame from the list of strings. For example as a result I will have, if I delete the word "Hello" I will get:

  [[1]]    
    [1] all    
    [2] the    
    [3] world
   [[2]]
    [1] world 
    [2] my  
    [3] Hi

PS: Another example of @Julius

 (l <- list(letters[1:2], letters[2:3]))

# [[1]]    
# [1] "a" "b"    
# [[2]]   
# [1] "b" "c" 

(r <- letters[2])

# [1] "b"

The result will be like this :

     [1] "c"

because all the letters before "b" with "b" are deleted.

(l <- list(letters[1:2], letters[2:3]))
# [[1]]
# [1] "a" "b"
# 
# [[2]]
# [1] "b" "c"
(r <- letters[2])
# [1] "b"
lapply(l, setdiff, r)
# [[1]]
# [1] "a"
# 
# [[2]]
# [1] "c"

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