Whitespace string can't be replaced with NA in R

前端 未结 2 1440
刺人心
刺人心 2021-01-15 00:13

I want to substitute whitespaces with NA. A simple way could be df[df == \"\"] <- NA, and that works for most of the cells of my data frame....but not for ev

2条回答
  •  悲&欢浪女
    2021-01-15 01:17

    We need to use lapply instead of sapply as sapply returns a matrix instead of a list and this can create problems in the quotes.

    df[1:10] <- lapply(df[1:10], trimws)
    

    and another option if we have spaces like " " is to use gsub to replace those spaces to ""

    df[1:10] <- lapply(df[,c(1:10)], function(x) gsub("^\\s+|\\s+$", "", x))
    

    and then change the "" to NA

    df[df == ""] <- NA
    

    Or instead of doing the two replacements, we can do this one go and change the class with type.convert

    df[] <- lapply(df, function(x)
          type.convert(replace(x, grepl("^\\s*$", trimws(x)), NA), as.is = TRUE))
    

    NOTE: We don't have to specify the column index when all the columns are looped

提交回复
热议问题