Skip to content

replace non numeric with "NA"

5 messages · Nandini B, James W. MacDonald, Duncan Murdoch

#
Hi Nandini,
On 4/29/2011 6:45 AM, Nandini B wrote:
> x <- data.frame(day=1:8, od = 
c(0.1,"#VALUE!",0.4,0.8,"-","s","--",19), month = c(2,1,12,10,3,7,12,7))
 > x
   day      od month
1   1     0.1     2
2   2 #VALUE!     1
3   3     0.4    12
4   4     0.8    10
5   5       -     3
6   6       s     7
7   7      --    12
8   8      19     7
 > x$od <- as.numeric(as.character(x$od))
Warning message:
NAs introduced by coercion
 > x
   day   od month
1   1  0.1     2
2   2   NA     1
3   3  0.4    12
4   4  0.8    10
5   5   NA     3
6   6   NA     7
7   7   NA    12
8   8 19.0     7


Best,

Jim

  
    
#
On 29/04/2011 6:45 AM, Nandini B wrote:
You don't tell use the types of the columns, so I'll assume they are 
factors.  If so, call

as.numeric(as.character())

on each of them to convert the number-like values to numbers, the others 
to NA.  For example,

df$day <- as.numeric(as.character(df$day))

Duncan Murdoch