简体   繁体   中英

How to avoid R converting F to False when converting logical vector to character

I've imported some data from my participants where some of my variables are F/M (female/male) and when I import it, R converts vectors with only F into a logical vector. When I then convert them back to character, the F has been transformed into FALSE. How can I avoid this F to FALSE transformation?

I know I could just transform all the FALSE back into F but I'd like to find an alternative solution to avoid my code looking cluttered.

This is my code for now and I suspect the issue is within lapply . I can't really give a full set of data since the command is incorporated into reading the csv files. I've give an example of a sample of what the data looks like in the CSV file vs. how it looks like when R has converted it. The actual data set has many more columns.

library(tidyverse)

csv_data <- data.frame(first = c(1, 1, 1, 1),
                first_sex = c("F", "F", "F", "F"),
                second = c(2, 2, 2, 2),
                second_sex = c("M", "F", "F", "F"))

R_output_data <- data.frame(first = c(1, 1, 1, 1),
                            first_sex = c(F, F, F, F),
                            second = c(2, 2, 2, 2),
                            second_sex = c("M", "F", "F", "F"))

files <- list.files(path = "path to data", 
                    pattern = "*.csv", full.names = T)

test_data <- lapply(files, read_csv) %>% 
  lapply(.,mutate_if, is.logical, as.character) %>%
  bind_rows()

If you know that the problematic columns are first_sex and second_sex , you can use the col_* handlers from readr . For example:

require(readr)    
notlogical<-cols(first_sex=col_character(),second_sex=col_character())
#then in the lapply:
test_data <- lapply(files, read_csv, col_types=notlogical) #the rest is the same

It doesn't feel very clean, but this type of process is what I was talking about in the comment. You do not need to specify specific column names (so it is somewhat flexible). But, if there are a couple columns causing the problem with the same names, that would be easier. Good luck!!

# Reading in all data as character using read_csv
test_data <- lapply(files, read_csv, col_types = cols(.default = "c"))

# using gsub to swap out f for female
test_data2 <- lapply(rapply(test_data, function(x) gsub("F|f", "Female", gsub("M|m", "Male", x)), 
how = "list"), as.data.frame, stringsAsFactors = F)

# Converting type for each dataframe in the list
final_data <- lapply(test_data2, type_convert)


# Checking if it worked
final_data[[1]]
  first first_sex second second_sex
1     1    Female      2       Male
2     1    Female      2     Female
3     1    Female      2     Female
4     1    Female      2     Female

sapply(final_data[[1]], class)
      first   first_sex      second  second_sex 
  "numeric" "character"   "numeric" "character" 

Data

csv_data <- data.frame(first = c(1, 1, 1, 1),
                       first_sex = c("F", "F", "F", "F"),
                       second = c(2, 2, 2, 2),
                       second_sex = c("M", "F", "F", "F"))


write_csv(csv_data, "csv_data.csv")
write_csv(csv_data, "csv2_data.csv")

files <- list.files(path = getwd(), 
                    pattern = "data.csv", full.names = T)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM