0

readr::type_convert guesses the class of each column in a data frame. I would like to apply type_convert to only some columns in a data frame (to preserve other columns as character). MWE:

# A data frame with multiple character columns containing numbers. df <- data.frame(A = letters[1:10], B = as.character(1:10), C = as.character(1:10)) # This works df %>% type_convert() Parsed with column specification: cols( A = col_character(), B = col_double(), C = col_double() ) A B C 1 a 1 1 2 b 2 2 ... 

However, I would like to only apply the function to column B (this is a stylised example; there may be multiple columns to try and convert). I tried using purrr::map_at as well as sapply, as follows:

# This does not work map_at(df, "B", type_convert) Error in .f(.x[[i]], ...) : is.data.frame(df) is not TRUE # This does not work sapply(df["B"], type_convert) Error in FUN(X[[i]], ...) : is.data.frame(df) is not TRUE 

Is there a way to apply type_convert selectively to only some columns of a data frame?

Edit: @ekoam provides an answer for type_convert. However, applying this answer to many columns would be tedious. It might be better to use the base::type.convert function, which can be mapped:

purrr::map_at(df, "B", type.convert) %>% bind_cols() # A tibble: 10 x 3 A B C <chr> <int> <chr> 1 a 1 1 2 b 2 2 

2 Answers 2

1

Try this:

df %>% type_convert(cols(B = "?", C = "?", .default = "c")) 

Guess the type of B; any other character column stays as is. The tricky part is that if any column is not of a character type, then type_convert will also leave it as is. So if you really have to type_convert, maybe you have to first convert all columns to characters.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks---this does work. Playing around with it, I've also found that it does not actually force everything else to be character; despite .default = "c", a non-selected int column remains int. Is it possible to specify more than one column to guess, though?
@simoncolumbus Thanks for the clarification. I have edited my post. Is this what you need?
I think this answer my question (although it means that type_convert isn't really useful for this kind of application, since converting many columns is tedious). Still, thanks a lot for the answer.
0

type_convert does not seem to support it. One trick which I have used a few times is using combination of select & bind_cols as shown below.

df %>% select(B) %>% type_convert() %>% bind_cols(df %>% select(-B)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.