简体   繁体   English

在将逻辑向量转换为字符时,如何避免R将F转换为False

[英]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. 我从参与者那里导入了一些数据,其中我的一些变量是F / M(女性/男性),当我导入它时,R将只有F的矢量转换为逻辑矢量。 When I then convert them back to character, the F has been transformed into FALSE. 当我将它们转换回角色时,F已经转换为FALSE。 How can I avoid this F to FALSE transformation? 我怎样才能避免这种F到FALSE的转换?

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. 我知道我可以把所有的FALSE转换回F但是我想找到一个替代解决方案来避免我的代码看起来混乱。

This is my code for now and I suspect the issue is within lapply . 这是我现在的代码,我怀疑问题是在lapply内。 I can't really give a full set of data since the command is incorporated into reading the csv files. 因为命令被合并到读取csv文件中,所以我无法真正提供完整的数据集。 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. 我举一个例子来说明CSV文件中数据的样子与R转换后的样子。 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 . 如果您知道有问题的列first_sexsecond_sex ,您可以使用col_*从处理器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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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