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.

sweetmusicality
  • 937
  • 1
  • 10
  • 27
  • 1
    Please provide a small reproducible example of your data – Dason Jun 20 '17 at 17:35
  • Possible duplicate of [R - How to test for character(0) in IF statement](https://stackoverflow.com/questions/25640161/r-how-to-test-for-character0-in-if-statement) – polka Jun 20 '17 at 17:45

1 Answers1

6

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

Val
  • 6,585
  • 5
  • 22
  • 52
  • yikes this almost worked but it only keeps the first element of each list when I want all of it retained – sweetmusicality Jun 20 '17 at 18:04
  • That's a problem with `ifelse`. Taking a conventional `if else` clause should do the trick. Please see my edits – Val Jun 20 '17 at 18:08
  • 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... – sweetmusicality Jun 20 '17 at 18:11
  • 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 similar – Val Jun 20 '17 at 18:52
  • I'm not entirely sure I know how to do that...could you guide me? – sweetmusicality Jun 22 '17 at 21:58
  • 2
    Just substitute the `x` after `else` with `paste0(x,collapse=' ')`. You can also use a comma or another separator in `collapse`. This should do it – Val Jun 22 '17 at 22:12