Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove row-specific items from list

Tags:

r

data.table

I would like to create the last column ('desired_result') from the 3 prior columns ('group', 'animal', and 'full'). Below is code for a reproducible example.

library(data.table)
data = data.table(group = c(1,1,1,2,2,2), animal = c('cat', 'dog', 'pig', 'giraffe', 'lion', 'tiger'), desired_result = c('dog, pig', 'cat, pig', 'cat, dog', 'lion, tiger', 'giraffe, tiger', 'giraffe, lion'))
data[, full := list(list(animal)), by = 'group']
data = data[, .(group, animal, full, desired_result)]

data
    group  animal               full   desired_result
1:     1     cat          cat,dog,pig     dog, pig
2:     1     dog          cat,dog,pig     cat, pig
3:     1     pig          cat,dog,pig     cat, dog
4:     2 giraffe   giraffe,lion,tiger    lion, tiger
5:     2    lion   giraffe,lion,tiger    giraffe, tiger
6:     2   tiger   giraffe,lion,tiger    giraffe, lion

Basically, I would like to modify 'full' so it does not include the corresponding 'animal'. I have tried various lapply commands using both list and character versions of these columns but have not been able to solve this problem.

like image 771
data_jay Avatar asked Jul 01 '15 21:07

data_jay


People also ask

How do I remove a specific part of a list?

There are three ways in which you can Remove elements from List: Using the remove() method. Using the list object's pop() method. Using the del operator.

How do I remove a row from a list?

DataFrame. drop() method you can remove/delete/drop the list of rows from pandas, all you need to provide is a list of rows indexes or labels as a param to this method. By default drop() method removes the rows and returns a copy of the updated DataFrame instead of replacing the existing referring DataFrame.

How do I remove a specific value from a list in Python?

How to Remove an Element from a List Using the remove() Method in Python. To remove an element from a list using the remove() method, specify the value of that element and pass it as an argument to the method. remove() will search the list to find it and remove it.

How do I remove a specific string from a list?

Use the list. pop() method to remove a string from a list, e.g. my_list. pop(0) .


2 Answers

Here's a possible approach

data[, desired_result := {
        temp <- unique(unlist(full)) 
        toString(temp[-match(animal, temp)])
        }, by = .(group, animal)]
data
#    group  animal               full desired_result
# 1:     1     cat        cat,dog,pig       dog, pig
# 2:     1     dog        cat,dog,pig       cat, pig
# 3:     1     pig        cat,dog,pig       cat, dog
# 4:     2 giraffe giraffe,lion,tiger    lion, tiger
# 5:     2    lion giraffe,lion,tiger giraffe, tiger
# 6:     2   tiger giraffe,lion,tiger  giraffe, lion
like image 132
David Arenburg Avatar answered Oct 16 '22 01:10

David Arenburg


Another option:

data[, desired := .(Map(setdiff, list(animal), as.list(animal))), by = group]

#or if starting from full
data[, desired := .(Map(setdiff, full, animal))]

(recycling magic makes the first version work)

like image 29
eddi Avatar answered Oct 16 '22 02:10

eddi