If else statement transformation

I am trying to create and if else statement for my dataset. I have a variable called 'freq' that has some values of 0. I want to turn those values of 0 into 10^-12 and then create a loglinear model. For the values of 'freq' that are greater than 0 I want to leave them as is. My attempts keep drawing errors.

if (freq==0) { print ("freq==10^-12")**
}
else print ("freq")

Here is an example

(mydataset <- data.frame(
  freq=0:2,
  otherthings = letters[1:3]
))

mydataset$freq2 <- ifelse(test = mydataset$freq==0,
                          yes = 10^-12,
                          no = mydataset$freq)

mydataset

A dplyr and case_when approach:

library(tidyverse)

freq <- tibble(
  freq = c(0, 12, 24, 0, 48, 0)
)


freq %>% 
  mutate(freq = case_when(freq == 0 ~ 10^(-12),
                          TRUE ~ freq))
#> # A tibble: 6 × 1
#>      freq
#>     <dbl>
#> 1 1  e-12
#> 2 1.2e+ 1
#> 3 2.4e+ 1
#> 4 1  e-12
#> 5 4.8e+ 1
#> 6 1  e-12

Created on 2021-10-18 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.