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

时间:2019-06-21 12:40:43

标签: r

我已经从我的参与者中导入了一些数据,其中一些变量是F / M(女性/男性),当我导入它时,R将只有F的向量转换为逻辑向量。当我将它们转换回字符时,F已转换为FALSE。如何避免这种从F到FALSE的转换?

我知道我可以将所有FALSE都转换回F,但是我想找到一个替代解决方案,以避免代码看起来混乱。

这是我现在的代码,我怀疑问题在lapply之内。由于命令已合并到读取csv文件中,因此我无法提供完整的数据集。我提供了一个示例示例,说明了CSV文件中数据的外观以及R对其进行转换后的外观。实际数据集还有更多列。

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()

2 个答案:

答案 0 :(得分:2)

如果您知道有问题的列是first_sexsecond_sex,则可以使用col_*中的readr处理程序。例如:

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

答案 1 :(得分:1)

感觉不是很干净,但是我在评论中正在谈论这种类型的过程。您无需指定特定的列名(因此有点灵活)。但是,如果有几列导致相同名称的问题,那会更容易。祝你好运!

# 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" 

数据

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)