[R] Help with data.frames

Heberto Ghezzo Heberto at meakins.lan.mcgill.ca
Tue Jul 25 18:39:25 CEST 2000


Hi  R experts, I have a problem. . .
I am using R 1.1 in Win98
if I do:

age <- c( 20,25,30,50,20,30)
sex <- c("M","F","F","M","M","F")
hgt <- c(1.80,1.65,1.70,1.75,1.85,1.68)
obs <-c("first","second","third","fourth","fifth","sixth")
base<-data.frame(rbind(age=age,sex=sex,hgt=hgt,obs=obs))
 base
  age sex  hgt    obs
1  20   M  1.8  first
2  25   F 1.65 second
3  30   F  1.7  third
4  50   M 1.75 fourth
5  20   M 1.85  fifth
6  30   F 1.68  sixth

i.e. I created a data.frame with I think, 2 real columns, 1 factor and 
1 character. . . I thought

lage<-log(age)
base2<-cbind(base,lage)
base2
  age sex  hgt    obs     lage
1  20   M  1.8  first 2.995732
2  25   F 1.65 second 3.218876
3  30   F  1.7  third 3.401197
4  50   M 1.75 fourth 3.912023
5  20   M 1.85  fifth 2.995732
6  30   F 1.68  sixth 3.401197

I can add a numeric column with no problems. . .I thought

Now I want to add a new observation. .

 log(40)
[1] 3.688879
 new.guy <- c(40,"M",1.82,"seventh",3.688879)
 base<-rbind(base,new.guy)
Warning messages: 
1: invalid factor level, NAs generated in: [<-.factor(*tmp*, ri, value = 
"40") 
2: invalid factor level, NAs generated in: [<-.factor(*tmp*, ri, value = 
"1.82") 
 
 base$age
[1] 20 25 30 50 20 30 NA
Levels:  20 25 30 50 
 base$sex
[1] M F F M M F M
Levels:  F M 
 base$age
[1] 20 25 30 50 20 30 NA
Levels:  20 25 30 50 
 base$lage
[1] 2.99573227355399 3.2188758248682  3.40119738166216 
3.91202300542815
[5] 2.99573227355399 3.40119738166216 3.688879 
Levels:  2.99573227355399 3.2188758248682 3.40119738166216 
3.688879 3.91202300542815 
>
i.e. I can not add a new value of age, All are factors!!! including 
log(age)! why?
	
so now I use the I construct as in the help

 base<-data.frame(cbind(age=I(age),sex=I(sex),hgt=I(hgt)))
> base
  age sex  hgt
1  20   M  1.8
2  25   F 1.65
3  30   F  1.7
4  50   M 1.75
5  20   M 1.85
6  30   F 1.68

> new.guy<-c(40,"B",1.82)
> base2<-rbind(base,new.guy)
Warning messages: 
1: invalid factor level, NAs generated in: [<-.factor(*tmp*, ri, value = 
"40") 
2: invalid factor level, NAs generated in: [<-.factor(*tmp*, ri, value = 
"B") 
3: invalid factor level, NAs generated in: [<-.factor(*tmp*, ri, value = 
"1.82") 
> is.factor(base$age)
[1] TRUE
>
Any help in how to turn all numeric factors into numeric, character 
vectors into character and
how to add new observations to a data.frame .
As always, many thanks to the members of the list.
.

R. Heberto Ghezzo  Ph.D.
Meakins-Christie Labs
McGill University
Montreal - Canada
heberto at meakins.lan.mcgill.ca
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list