pssguy
pssguy

Reputation: 3515

How can I use purrr to limit rows where column element is not a list

I have a data.frame,df, where one of the columns has entries which are either a character or list

enter image description here

I would like to use the purrr package, or other means, to eliminate the second row

df <- structure(list(member_id = c("1715", "2186", "2187"), date_of_birth = list(
"1953-12-15T00:00:00", structure(list(`@xsi:nil` = "true", 
    `@xmlns:xsi` = "http://www.w3.org/2001/XMLSchema-instance"), .Names = c("@xsi:nil", 
"@xmlns:xsi")), "1941-02-16T00:00:00")), .Names = c("member_id", 
"date_of_birth"), row.names = c(1L, 8L, 9L), class = "data.frame")

TIA

Upvotes: 0

Views: 49

Answers (2)

Abdou
Abdou

Reputation: 13274

If you are looking to drop any row whose date_of_birth field is of type list, the following should be a decent solution:

df[sapply(df$date_of_birth, function(x) typeof(x)!="list"),]

Edit:

Imo's comment should shorten the above solution as follows:

df[!sapply(df$date_of_birth, is.list),]

I hope this helps.

Upvotes: 1

lmo
lmo

Reputation: 38510

Here is base R method using lengths and subsetting. Any element in the date_of_birth column that has more than one element is dropped

dfNew <- df[lengths(df$date_of_birth) < 2,]

which returns

dfNew
  member_id       date_of_birth
1      1715 1953-12-15T00:00:00
9      2187 1941-02-16T00:00:00

Note that dfNew$date_of_birth will still be of type list, which might cause problems down the line. You can fix this with unlist.

dfNew$date_of_birth <- unlist(dfNew$date_of_birth)

Upvotes: 1

Related Questions