# Make a data.frame:
df <- data.frame(a = c("yes", "no", "yes", "yes", "no",
"yes", "yes", "no", "yes"),
b = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
c = c("apple", "kiwi", "banana", "apple", "kiwi",
"banana", "apple", "kiwi", "banana"),
d = c(1.1, 1.1, 3.4, 4.5, 5.4, 6.7, 7.8, 8.1, 9.8)
)
str(df)
# Convert specified columns to factors:
df1 <- f_factors(df, select = c("a", "c"))
str(df1)
# Convert all potential factor columns to factor but exclude column "b":
df2 <- f_factors(df, exclude = c("b"))
str(df2)
# Convert all columns to factor but exclude column "b":
df3 <- f_factors(df, exclude = c("b"), force_factors = TRUE)
str(df3)
# Or automatically detect and convert suitable columns to factors.
# In this example obtaining the same results as above automatically
# and storing it in df2:
df4 <- f_factors(df)
str(df4)
# In example above col b was converted to a factor as the number of repeats = 2
# and the amount of unique numbers < 8. In order to keep b numeric we can also
# adjust the unique_num_treshold and/or repeats_threshold:
df5 <- f_factors(df, unique_num_treshold = 2)
str(df5)
Run the code above in your browser using DataLab