Cloud
Cloud

Reputation: 53

convert all columns from logical to characters in a csv

I'm trying to convert all the columns that appear as logical when importing a csv to characters.

These code bellow works if I call every column, but I have over 200 columns so its not feasible.

library(stringi)
library(readr)
library(dplyr)

df <- read_csv("~/dataverified.csv", 
col_types = cols(
Innovation = col_character(), 
Tech = col_character(), 
Music = col_character()))

Here is sample of the summary () for one column and bellow is the desire output

e.g summary of input

Innovation                                    
Mode:logical
TRUE:403
NA's 45600

e.g summary of desire output for every column

Innovation
Length: 45900                                    
Class:character
Mode:character

Upvotes: 4

Views: 9911

Answers (2)

www
www

Reputation: 39154

After you read your dataset, we can use the mutate_if function from the package. mutate_if can check if the column is logical with is.logical function. If TRUE, we can then use the as.character function.

# Load package
library(dplyr)

# Create example data frame
df <- data_frame(A = as.logical(c(1, 1, 0)),
                 B = as.logical(c(1, 0, 1)))

summary(df)
#     A               B          
# Mode :logical   Mode :logical  
# FALSE:1         FALSE:1        
# TRUE :2         TRUE :2

# Convert logical to character
df2 <- df %>%
  mutate_if(is.logical, as.character)

summary(df2)
#      A                  B            
# Length:3           Length:3          
# Class :character   Class :character  
# Mode  :character   Mode  :character  

If you are certain that all columns should be character, you can also consider the mutate_all function.

df2 <- df %>%
  mutate_all(funs(as.character(.)))

Upvotes: 9

Cybernetic
Cybernetic

Reputation: 13334

Dataframe with 45900 rows and 200 logical columns:

df <- data.frame(matrix(nrow = 45900, ncol = 200))

Confirming columns are logical type:

lapply(df[1:10], class)

enter image description here

Make some columns numeric to test approach:

df$X1 <- as.numeric(df$X1)
df$X2 <- as.numeric(df$X2)
df$X7 <- as.numeric(df$X7)

Write function that converts any logical columns to character type:

df_2 <- data.frame(lapply(df, function(x) if(is.logical(x)) { 
     return(as.character(x))
     } else {  
     return(x) 
     }
), stringsAsFactors=FALSE)

Validate conversion worked:

lapply(df_2[1:10], class)

enter image description here

Upvotes: 4

Related Questions