Unlist a column while retaining character(0) as empty strings in R
Asked Answered
J

1

4

I am relatively new to R. I have a dataframe that has a column stored as a list. My column contain c("Benzo", "Ferri") or character(0) if it's empty. How can I change them to simply Benzo, Ferri and an empty string for character(0) instead?

I'm not able to, for instance df$general_RN <- unlist(df$general_RN) because Error in $<-.data.frame(*tmp*, general_RN, value = c("Drug Combinations", : replacement has 1992 rows, data has 10479

I am assuming that all the character(0) have been removed but I need them retained as NAs.

Here is what the column looks like

general_RN
c("Chlorambucil", "Vincristine", "Cyclophosphamide")
Pentazocine
character(0)
character(0)
c("Ampicillin", "Trimethoprim")
character(0)

I have ashamedly spent an hour on this problem.

Thanks for your advice.

Jacquijacquie answered 20/6, 2017 at 17:30 Comment(2)
Please provide a small reproducible example of your dataAshur
Possible duplicate of R - How to test for character(0) in IF statementBowles
C
7

It's tough to say without more information about your data, but maybe this can be a solution for you, or at least point you into the right direction:

a <- list('A',character(0),'B')

> a
[[1]]
[1] "A"

[[2]]
character(0)

[[3]]
[1] "B"

> unlist(lapply(a,function(x) if(identical(x,character(0))) ' ' else x))
[1] "A" " " "B"

So in your case that should be:

df$general_RN <- unlist(lapply(df$general_RN,function(x) if(identical(x,character(0))) ' ' else x))

HTH

Cynthy answered 20/6, 2017 at 17:48 Comment(6)
yikes this almost worked but it only keeps the first element of each list when I want all of it retainedJacquijacquie
That's a problem with ifelse. Taking a conventional if else clause should do the trick. Please see my editsCynthy
thanks. however, that gives me this error Error in $<-.data.frame(*tmp*, general_RN, value = c(" ", " ", " ", : replacement has 11066 rows, data has 10479 I don't know how or why the replacement has more rows than the data...Jacquijacquie
Looking at the example of your column, it's clear that after unlisting you'll have more values than rows in the columns. A vector of 3 strings will be taking 3 individual rows in the end. What you could do is combining the individual stings to a single one with the "words" separated by spaces or similarCynthy
I'm not entirely sure I know how to do that...could you guide me?Jacquijacquie
Just substitute the x after else with paste0(x,collapse=' '). You can also use a comma or another separator in collapse. This should do itCynthy

© 2022 - 2024 — McMap. All rights reserved.