[R] Problems with variable types.

Ott Toomet otoomet at econ.dk
Fri Mar 7 10:01:00 CET 2003


Dear Ronaldo,

Is your problem to create a data frame where character vectors remain
characters, not factors?

In documentation of as.data.frame you can read:

Character variables passed to data.frame are converted to factor
columns unless protected by I. It also applies to adding columns to
a data frame.

So you should create your data frame like

dados.fim <- data.frame(L, A, Nsp, N, I(AmT), I(NAm), I(AMST))

I don't know what is the mode of dados.fin, but probably something
happens there too.  

Best wishes,

Ott  

 | From: "Ronaldo Reis Jr." <chrysopa at insecta.ufv.br>
 | Date: Thu, 6 Mar 2003 11:46:28 -0300
 | 
 | Hi all,
 | 
 | I have problems in a dataframe variables types.
 | 
 | Look:
 | 
 | from a loop function:
 | for(...){
 | ...
 | dados.fin <- rbind(dados.fin, c(L=j, A=j^2,
 |                                 Nsp=nsps,
 |                                 N=length(amosfin$SP),
 |                                 AmT="am",NAm=nam,
 |                                 AMST=amst))
 | dados.fin <- rbind(dados.fin, c(L=j, A=j^2,
 |                                 Nsp=nsp, 
 |                                 N=nbicho, AmT="tot",
 |                                 NAm=nam,
 |                                 AMST=amst))
 | ...
 | }
 | 
 |     dados.fin <- as.data.frame(dados.fin)
 | 
 | > summary(dados.fin)
 |   L        A           Nsp           N       AmT     NAm      AMST   
 |  10:10   100:10   11     : 7   12     : 5   am :30   5:60   unif:60  
 |  12:10   144:10   7      : 7   122    : 5   tot:30                   
 |  14:10   196:10   16     : 5   181    : 5                            
 |  16:10   256:10   25     : 5   270    : 5                            
 |  18:10   324:10   37     : 5   403    : 5                            
 |  20:10   400:10   55     : 5   55     : 5                            
 |                   (Other):26   (Other):30                            
 | 
 | All variables appear like factors, but only AmT and AMST are really factors.
 | 
 | I try to use 
 | 
 | dados.fin <- rbind(dados.fin, c(L=as.numeric(j), A=j^2,
 |                                 Nsp=nsp, 
 |                                 N=nbicho, AmT="tot",
 |                                 NAm=nam,
 |                                 AMST=amst))
 | 
 | and some combinations like as.numeric(as.character(j)) etc.
 | 
 | How I make to force the types?



More information about the R-help mailing list