[R] Converting text to numbers
jim holtman
jholtman at gmail.com
Wed Sep 27 23:33:17 CEST 2006
> x <- read.table('clipboard', header=T, as.is=T)
> str(x)
`data.frame': 7 obs. of 2 variables:
$ FCT: chr "1" "2" "3-" "3" ...
$ OBS: chr "5" "4" "3+" "3" ...
# define your conversion
> x.c <- c('1'=1, '2'=2, '3'=3, '3-'=2, '3+'=5, '4-'=3, '4'=6, '5'=7)
> x.c[x$FCT]
1 2 3- 3 3+ 4 5
1 2 2 3 5 6 7
> x$FCT1 <- x.c[x$FCT]
> x$OBS1 <- x.c[x$OBS]
> x
FCT OBS FCT1 OBS1
1 1 5 1 7
2 2 4 2 6
3 3- 3+ 2 5
4 3 3 3 3
5 3+ 3- 5 2
6 4 2 6 2
7 5 1 7 1
> str(x)
`data.frame': 7 obs. of 4 variables:
$ FCT : chr "1" "2" "3-" "3" ...
$ OBS : chr "5" "4" "3+" "3" ...
$ FCT1: num 1 2 2 3 5 6 7
$ OBS1: num 7 6 5 3 2 2 1
> x$FCT1 - x$OBS1
[1] -6 -4 -3 0 3 4 6
>
On 9/27/06, Dan Chan <dchan at gfc.state.ga.us> wrote:
> Hi,
>
> I have Forecast Class and Observed Class in a data matrix as below.
>
> > Sample1
> FCT OBS
> 1 1 5
> 2 2 4
> 3 3- 3+
> 4 3 3
> 5 3+ 3-
> 6 4 2
> 7 5 1
>
> I want to find the difference between Observed and Forecast Classes.
> How can I get this done?
>
> I tried to following to convert the 1 through 5 classes, to 1 through 7
> for both OBS and FCT column.
> > Sample1$OBS2 <- Sample1$OBS
> > levels(Sample1$OBS2) <- sub('5',7,levels(Sample1$OBS2),fixed=TRUE)
> > levels(Sample1$OBS2) <- sub('4',6,levels(Sample1$OBS2),fixed=TRUE)
> > levels(Sample1$OBS2) <- sub('3+',5,levels(Sample1$OBS2), fixed=TRUE)
> > levels(Sample1$OBS2) <- sub('3',4,levels(Sample1$OBS2),fixed=TRUE)
> > levels(Sample1$OBS2) <- sub('4-',3,levels(Sample1$OBS2),fixed=TRUE)
> > Sample1
> FCT OBS FCT2 OBS2
> 1 1 5 1 7
> 2 2 4 2 6
> 3 3- 3+ 3 5
> 4 3 3 4 4
> 5 3+ 3- 5 3
> 6 4 2 6 2
> 7 5 1 7 1
>
> All looks good, but as I do the following, I encounter an error.
> > Sample1$OBS2- Sample1$FCT2
> [1] NA NA NA NA NA NA NA
> Warning message:
> - not meaningful for factors in: Ops.factor(Sample1$OBS2, Sample1$FCT2)
>
> Then, I tried to convert them to numbers using the following.
> > Sample1$FCT2 <- as.numeric(Sample1$FCT2)
> > Sample1$OBS2 <- as.numeric(Sample1$OBS2)
> > Sample1
> FCT OBS FCT2 OBS2
> 1 1 5 1 7
> 2 2 4 2 6
> 3 3- 3+ 4 5
> 4 3 3 3 3
> 5 3+ 3- 5 4
> 6 4 2 6 2
> 7 5 1 7 1
>
> Sample1$FCT2[3] and Sample1$FCT2[4] switched values.
>
> I think it has something to do with the following:
> > Sample1$OBS
> [1] 5 4 3+ 3 3- 2 1
> Levels: 1 2 3 3- 3+ 4 5
>
> But, I don't know why and how to fix it.
>
> Any ideas?
>
> Thank you.
>
>
>
> Daniel Chan
> Meteorologist
> Georgia Forestry Commission
> P O Box 819
> Macon, GA
> 31202
> Tel: 478-751-3508
> Fax: 478-751-3465
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list