Re: [R] replace non numeric with NA
Hi Nandini, On 4/29/2011 6:45 AM, Nandini B wrote: Hello, I have a sample data frame which looks like this day od month 1 1 0.12 2 3 #VALUE! 1 3 5 0.4 12 4 7 0.8 10 5 11 - 3 6 14 s 7 7 18 -- 12 8 27 197 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.412 4 4 0.810 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.412 4 4 0.810 5 5 NA 3 6 6 NA 7 7 7 NA12 8 8 19.0 7 Best, Jim Now i wish to filter all the non numeric values and replace it with NA. The data frame is actually huge and the non numeric characters vary from - to a string to absolutely anything!!! Can anyone please help ? Thank you, Warm Regards, Nandini [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- James W. MacDonald, M.S. Biostatistician Douglas Lab University of Michigan Department of Human Genetics 5912 Buhl 1241 E. Catherine St. Ann Arbor MI 48109-5618 734-615-7826 ** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replace non numeric with NA
On 29/04/2011 6:45 AM, Nandini B wrote: Hello, I have a sample data frame which looks like this day od month 1 1 0.12 2 3 #VALUE! 1 3 5 0.4 12 4 7 0.8 10 5 11 - 3 6 14 s 7 7 18 -- 12 8 27 197 Now i wish to filter all the non numeric values and replace it with NA. The data frame is actually huge and the non numeric characters vary from - to a string to absolutely anything!!! Can anyone please help ? 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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replace non numeric with NA
Thanks a lot Jim, this is perfect!! Thank you, Nandini Badarinarayan Date: Fri, 29 Apr 2011 09:49:26 -0400 From: jmac...@med.umich.edu To: nandini...@hotmail.com CC: r-help@r-project.org Subject: Re: [R] replace non numeric with NA Hi Nandini, On 4/29/2011 6:45 AM, Nandini B wrote: Hello, I have a sample data frame which looks like this day od month 1 1 0.12 2 3 #VALUE! 1 3 5 0.4 12 4 7 0.8 10 5 11 - 3 6 14 s 7 7 18 -- 12 8 27 197 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.412 4 4 0.810 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.412 4 4 0.810 5 5 NA 3 6 6 NA 7 7 7 NA12 8 8 19.0 7 Best, Jim Now i wish to filter all the non numeric values and replace it with NA. The data frame is actually huge and the non numeric characters vary from - to a string to absolutely anything!!! Can anyone please help ? Thank you, Warm Regards, Nandini [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- James W. MacDonald, M.S. Biostatistician Douglas Lab University of Michigan Department of Human Genetics 5912 Buhl 1241 E. Catherine St. Ann Arbor MI 48109-5618 734-615-7826 ** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replace non numeric with NA
Thanks a lot Duncan, this is what I was looking for!!Thank you,Nandini Date: Fri, 29 Apr 2011 09:53:06 -0400 From: murdoch.dun...@gmail.com To: nandini...@hotmail.com CC: r-help@r-project.org Subject: Re: [R] replace non numeric with NA On 29/04/2011 6:45 AM, Nandini B wrote: Hello, I have a sample data frame which looks like this day od month 1 1 0.12 2 3 #VALUE! 1 3 5 0.4 12 4 7 0.8 10 5 11 - 3 6 14 s 7 7 18 -- 12 8 27 197 Now i wish to filter all the non numeric values and replace it with NA. The data frame is actually huge and the non numeric characters vary from - to a string to absolutely anything!!! Can anyone please help ? 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 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.