Converting from a character to a numeric data frame

后端 未结 2 1424
-上瘾入骨i
-上瘾入骨i 2021-01-04 18:16

I have a character data frame in R which has NaNs in it. I need to remove any row with a NaN and then convert it to a numeric data frame.

I

2条回答
  •  庸人自扰
    2021-01-04 18:54

    As @thijs van den bergh points you to,

    dat <- data.frame(x=c("NaN","2"),y=c("NaN","3"),stringsAsFactors=FALSE)
    
    dat <- as.data.frame(sapply(dat, as.numeric)) #<- sapply is here
    
    dat[complete.cases(dat), ]
    #  x y
    #2 2 3
    

    Is one way to do this.

    Your error comes from trying to make a data.frame numeric. The sapply option I show is instead making each column vector numeric.

提交回复
热议问题