convert.factor: Converts from factors to character.

Description Usage Arguments Reuse good code Alternative See Also

Description

Converts any character fields in a dataframe from factors to character fields.

Usage

1

Arguments

r

A dataframe.

Reuse good code

"The single biggest way to improve both the quality of your code and your productivity is to reuse good code (...) Many algorithms have already been invented, tested, discussed in the trade literature, reviewed, and improved".

–Code complete 2, by Steve McConnell.

Alternative

A more powerful and general alternative is dplyr::mutate_if():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
lett <- letters[1:3]
x <- tibble::tibble(
  was_fctr = as.factor(lett), 
  was_chr = lett, 
  was_int = as.integer(1:length(lett)),
  was_dbl = as.numeric(1:length(lett)) 
  )

x
#> # A tibble: 3 x 4
#>   was_fctr was_chr was_int was_dbl
#>     <fctr>   <chr>   <int>   <dbl>
#> 1        a       a       1       1
#> 2        b       b       2       2
#> 3        c       c       3       3

dplyr::mutate_if(x, is.factor, as.character)
#> # A tibble: 3 x 4
#>   was_fctr was_chr was_int was_dbl
#>      <chr>   <chr>   <int>   <dbl>
#> 1        a       a       1       1
#> 2        b       b       2       2
#> 3        c       c       3       3

dplyr::mutate_if(x, is.double, as.integer)
#> # A tibble: 3 x 4
#>   was_fctr was_chr was_int was_dbl
#>     <fctr>   <chr>   <int>   <int>
#> 1        a       a       1       1
#> 2        b       b       2       2
#> 3        c       c       3       3

See Also

dplyr::mutate_if().


forestgeo/ctfs documentation built on May 3, 2019, 6:44 p.m.