1

I have a dataframe that looks like this:

df1 <- structure(list(Name = c("Mark", "Anders", "Tom", "Vin", "Marcel", 
"Tyta", "Gerta", "Moses", "Hank", "Rita", "Margary"), Col = c(1769380097.5, 
1444462500, 1499146687.5, 1276309375, 22279500, 3114023471, 2961012500, 
3978937423.5, 1703925000, 1838885550, 0), item1 = c(1534931323.07692, 
1794881375, 2292661687.5, 855786250, 21915500, 3056061512.25, 
3581940000, 3766909703.25, 2043300000, 2135859875, 1482031250
), item2 = c(1628137500, 1781982737.5, 1659391250, 741220687.5, 
41242000, 2833327766.38514, 3675450000, 3592650662.5, 1586512500, 
1934575000, 1467271250), item3 = c(1545572702.88461, 1748600000, 
1745026687.5, 1556481250, 0, 3551716021.25, 3108137500, 3718036445, 
1380278750, 2217526000, 1026813750)), .Names = c("Name", "Col", 
"dKO1", "dKO2", "sdi1"), row.names = c(29L, 30L, 1278L, 1295L, 
1296L, 1297L, 1298L, 1307L, 1642L, 1674L, 1754L), class = "data.frame")

> df1
        Name        Col       dKO1       dKO2       sdi1
29      Mark 1769380098 1534931323 1628137500 1545572703
30    Anders 1444462500 1794881375 1781982738 1748600000
1278     Tom 1499146688 2292661688 1659391250 1745026688
1295     Vin 1276309375  855786250  741220688 1556481250
1296  Marcel   22279500   21915500   41242000          0
1297    Tyta 3114023471 3056061512 2833327766 3551716021
1298   Gerta 2961012500 3581940000 3675450000 3108137500
1307   Moses 3978937424 3766909703 3592650662 3718036445
1642    Hank 1703925000 2043300000 1586512500 1380278750
1674    Rita 1838885550 2135859875 1934575000 2217526000
1754 Margary          0 1482031250 1467271250 1026813750

Some values are zeros. I want to add 0.00001 to those numbers across the entire dataframe. Then, i want to log transform all the columns, excluding the first one. How can I do this?

Workhorse
  • 1,500
  • 1
  • 17
  • 27

1 Answers1

1

Using dplyr you have to do two things to get your desired result:

df1[df1==0] <- 0.00001
df1 %>%
  mutate_at(vars(-Name), log)

But since you want to use a log on your columns I advice to skip the df1[df1==0] <- 0.00001 part and replace -Inf created by log with 0 or NA or whatever value you need.

Martin Gal
  • 16,640
  • 5
  • 21
  • 39