na.strings applied to a dataframe

前端 未结 4 520
無奈伤痛
無奈伤痛 2021-01-18 07:41

I currently have a dataframe in which there are several rows I would like converted to \"NA\". When I first imported this dataframe from a .csv, I could use na.strings=c(\"A

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 07:51

    Just assign the NA values directly.

    e.g.:

    x <- data.frame(a=1:5, b=letters[1:5])
    # > x
    #   a b
    # 1 1 a
    # 2 2 b
    # 3 3 c
    # 4 4 d
    # 5 5 e
    
    # convert the 'b' and 'd' in columb b to NA
    x$b[x$b %in% c('b', 'd')] <- NA
    # > x
    #  a     b
    # 1 1    a
    # 2 2 
    # 3 3    c
    # 4 4 
    # 5 5    e
    

提交回复
热议问题