0

I want to calculate the logarithm of total assets, and I want r to set the result to NULL if the total assets element itself is 0. Otherwise, r will show me -Inf as the result. I have tried to solve this problem as follows:

at <- c(0.028, 0, 0, 0, 0, 0)
name <- c("comp 1","comp2", "comp3", "comp4", "comp5", "comp6")
df <- as.data.frame(cbind(name, at)) 
df$log_at <- ifelse(is.null(at),  0, log(at))

However, if you run this line of code, the column 2 with the log(assets) will then show -3.575551 instead of 0 in each row of the data frame for the NULL values of the total assets. Also, I wanted to know how many NULL values are in the total assets column, but:

sum(is.null(df$at))

will give me 0, so I am wondering why r does not identify the Null values as such, since the values in the total assets column are all numeric.

I know, I can use the following as an alternative since the amounts of total assets are pretty big but I am wondering why the code above does not work.

df$at <- as.numeric(df$at)
df$log_at <-  log(df$at + 1)

I hope someone can help me out !

wiwi123
  • 3
  • 2
  • 1
    Can you `dput` your data, so we can provide you a solution? You current code works 'as intended' just not how you want it to work. You want run your codes rowwise, and this is not what you are doing. – Serkan Jun 21 '21 at 15:34
  • I presume you may be looking for either `df$at == ''` or `is.na(df$at)` – AnilGoyal Jun 21 '21 at 15:37
  • I am not familiar with dput, but my dataset contains over 250000 observations with 36 variables, and has approximately 8GB, don't know if that plays a role@Serkan – wiwi123 Jun 21 '21 at 15:47
  • 1
    We need a [mcve] please; please read https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for lots of suggestions about how to set up a reproducible example. – Ben Bolker Jun 21 '21 at 15:48
  • is that sufficient ? – wiwi123 Jun 21 '21 at 16:45

1 Answers1

1

NULL is not 0. Check:

is.null(df$at)
[1] FALSE

So if you want to transform atusing ifelsethen it should be:

df$log_at <- ifelse(at == 0,  0, log(at))

Likewise, to find out how often at is 0:

sum(df$at == 0)
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34