0
votes

I am trying to work using ROracle to query the Oracle database within R.

I have been able to store a table in an R data.frame, though I am facing the following issue.

Assuming the following data.frame,

my_table <- data.frame(name = c("a","b"), surname = c("h", "i"), stuff = c("as", "qweq"))

My objective is to find a completely automated line(s) of code that convert to factor all columns that are characters and which name do not contain the string "name".

In this case, only stuff should be converted to factor.

2

2 Answers

1
votes

A dplyr solution: mutate_at takes select helper functions such as tidyselect::contains, which searches column names for a string and operates only on those. There's also tidyselect::matches, which is similar but takes a regex. In this case, since you wanted columns that don't have this string, I negated those column positions.

Note also that the default of data.frame is to make character vectors into factors, so these are already all factors.

library(tidyverse)

my_table <- data.frame(name = c("a","b"), surname = c("h", "i"), stuff = c("as", "qweq"), 
                       stringsAsFactors = F)

my_table %>%
  mutate_at(vars(-contains("name")), as.factor) %>%
  as_tibble()
#> # A tibble: 2 x 3
#>   name  surname stuff
#>   <chr> <chr>   <fct>
#> 1 a     h       as   
#> 2 b     i       qweq

You can omit the as_tibble; I did that just to get the printout with the column types included.

0
votes

There is more than one way to do this, here is a quick example in base R:

my_table <- data.frame(name = c("a","b"), surname = c("h", "i"), stuff = c("as", "qweq"), stringsAsFactors = FALSE)

my_table[, -(grep("name", colnames(my_table)))] <- as.factor(my_table[, -(grep("name", colnames(my_table)))])