Home > database >  Why does R format function not work as expected in dataframe?
Why does R format function not work as expected in dataframe?

Time:01-13

The format() function is not working as expected within the dataframe using dpylr and mutate(). I was attempting to edit values to insert into text for automated reporting. The nsmall parameter for the format() function appears to come from the first row only rather than the relevant row. What am I missing?

df <- data.frame (Value  = c(10/3, 10/9, 1/3, 5/4),
                  DataType = c("Num","Num","Percent","Percent"),
                  Decimals = c(3,1,0,1))
df <- df %>%
  mutate(String = format(round(Value, digits=Decimals), nsmall=Decimals),
         Output = if_else(DataType == "Num", String, paste0(String," %")))
df

Result:

> df
      Value DataType Decimals String  Output
1 3.3333333      Num        3  3.333   3.333
2 1.1111111      Num        1  1.100   1.100
3 0.3333333  Percent        0  0.000 0.000 %
4 1.2500000  Percent        1  1.200 1.200 %

Expected result:

> df
      Value DataType Decimals String  Output
1 3.3333333      Num        3  3.333   3.333
2 1.1111111      Num        1  1.1     1.1
3 0.3333333  Percent        0  0       0 %
4 1.2500000  Percent        1  1.2     1.2 %

CodePudding user response:

df <- df %>%
  rowwise() %>%
  mutate(String = format(round(Value, digits=Decimals), nsmall=Decimals),
         Output = if_else(DataType == "Num", String, paste0(String," %")))

rowwise() makes everything go a row at a time. There may be a vectorised way to do what you want, but this is a simple approach.

> df
# A tibble: 4 × 5
# Rowwise: 
  Value DataType Decimals String Output
  <dbl> <chr>       <dbl> <chr>  <chr> 
1 3.33  Num             3 3.333  3.333 
2 1.11  Num             1 1.1    1.1   
3 0.333 Percent         0 0      0 %   
4 1.25  Percent         1 1.2    1.2 % 
  • Related