I am trying to create the log of multiple variables in a dataframe which includes also non numeric variables, and would like to apply the function only to those numeric variables which include no zeros or negative values.
This is where I am at:
# creating a df with numeric and factor variables
a <- c(3, -1, 0, 5, 2)
b <- c(1, 3, 2, 1, 4)
c <- c(9, -2, 3, -5, 1)
d <- c(3, 0, 6, 1, 5)
e <- c("red", "blu", "yellow", "green", "white")
f <- c(0, 1, 1, 0, 0)
g <- c(3, 1, 1, 4, 2)
df <- data.frame(a,b,c,d,e,f,g) %>%
mutate_at("f",factor)
#applying the transformation to all numeric variables
df.log <- df %>%
as_tibble() %>%
mutate(across(
.cols = is.numeric, #& all()>0,#ideally I shall add here the condition '& >0' but it doesn't work
.fns = list(log = log),
.names = "{.col}_{.fn}"))
With the code above I have NaN for negative values and -inf for zeros. I could then drop columns with those values, but I'd like to find a clean way to do it all at once.
Another idea was to remove columns with values <=0 before as follows:
df.skim <- df %>%
select_if(is.numeric)
df.skim <- df.skim[,sapply(df.skim, min)>0]
and then apply the log to the columns left, but in this way I drop also the key column and I cannot easily merge back the data.