Lock column by storing character (0) as empty rows in R

I'm relatively new to R. I have a data framework that has a column stored as a list. My column contains c("Benzo", "Ferri")or character(0)if it is empty. How can I change them simply Benzo, Ferriand empty string for character (0) instead?

I can’t, for example df$general_RN <- unlist(df$general_RN), becauseError in $<-.data.frame(*tmp*, general_RN, value = c("Drug Combinations", : replacement has 1992 rows, data has 10479

I assume that all character(0)were deleted, but I need them to be saved as NAs.

Here is a column

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

I was ashamed to spend an hour on this problem.

Thanks for your advice.

+1
source share
1 answer

, , , , , , :

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"

, :

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

+3

Source: https://habr.com/ru/post/1696261/


All Articles