Hi @fary,
The following code shows you how to build a simple user-defined function to do what you requested. This is only one of many possible ways to achieve the same result.
Note that the table() function does not "change" anything; it simply reports a frequency table across the vector.
df <- data.frame(a = c(1,2,2,0,0,4,5,2,7,8),
b = c(11,14,0,0,15,18,18,0,19,20))
df
#> a b
#> 1 1 11
#> 2 2 14
#> 3 2 0
#> 4 0 0
#> 5 0 15
#> 6 4 18
#> 7 5 18
#> 8 2 0
#> 9 7 19
#> 10 8 20
df_old <- df # Save original fro later use.
# Check whether this code gives us what we need?
lapply(df, table) # This works because a 'dataframe' is also a 'list' of columns.
#> $a
#>
#> 0 1 2 4 5 7 8
#> 2 1 3 1 1 1 1
#>
#> $b
#>
#> 0 11 14 15 18 19 20
#> 3 1 1 1 2 1 1
df[df == 0] <- NA # This overwrites the original dataframe; works across all columns.
df
#> a b
#> 1 1 11
#> 2 2 14
#> 3 2 NA
#> 4 NA NA
#> 5 NA 15
#> 6 4 18
#> 7 5 18
#> 8 2 NA
#> 9 7 19
#> 10 8 20
# OK, now put those commands in a user-defined function.
my_fun <- function(df) {
lst <- lapply(df, table)
print(lst)
df[df == 0] <- NA
return(df)
}
# Run the function with a dataframe name as the input to the first argument.
# In this case we need to run this using the original df specification.
my_fun(df_old)
#> $a
#>
#> 0 1 2 4 5 7 8
#> 2 1 3 1 1 1 1
#>
#> $b
#>
#> 0 11 14 15 18 19 20
#> 3 1 1 1 2 1 1
#> a b
#> 1 1 11
#> 2 2 14
#> 3 2 NA
#> 4 NA NA
#> 5 NA 15
#> 6 4 18
#> 7 5 18
#> 8 2 NA
#> 9 7 19
#> 10 8 20
Created on 2021-07-04 by the reprex package (v2.0.0)