[R] converting blank cells to NAs
arun
smartpink111 at yahoo.com
Tue Apr 16 04:13:52 CEST 2013
You can use na.strings="" in read.table() or read.csv()
library(stringr)
vec1<-unlist(str_split(readLines(textConnection("3,7,11,,12,14,15,,17,18,19")),","))
vec1[vec1==""]<- NA
vec1
# [1] "3" "7" "11" NA "12" "14" "15" NA "17" "18" "19"
#or
scan(text="3,7,11,,12,14,15,,17,18,19",sep=",")
#Read 11 items
#[1] 3 7 11 NA 12 14 15 NA 17 18 19
set.seed(25)
arr1<- array(sample(letters,40,replace=TRUE),dim=c(5,4,2))
arr1[5,3,2]<-""
arr1[arr1==""]<- NA
arr1[,,2]
# [,1] [,2] [,3] [,4]
#[1,] "b" "h" "v" "k"
#[2,] "l" "b" "j" "y"
#[3,] "c" "p" "g" "j"
#[4,] "e" "n" "c" "e"
#[5,] "h" "g" NA "d"
arr2<- array(1:40,dim=c(5,4,2))
arr2[5,3,2]<- ""
arr2[arr2==""]<- NA
arr2<- apply(arr2,c(2,3),as.numeric)
arr2[,,2]
# [,1] [,2] [,3] [,4]
#[1,] 21 26 31 36
#[2,] 22 27 32 37
#[3,] 23 28 33 38
#[4,] 24 29 34 39
#[5,] 25 30 NA 40
A.K.
>Is there a way to convert blank cells in a vector or array into "NA"s?
Alternatively, is there a test for blank cells the way one can use
is.na to test for >NAs?
>
>Thanks in advance.
More information about the R-help
mailing list