Skip to content

Dataframes and text identifier columns

3 messages · Brian Willis, David Winsemius, Richard M. Heiberger

#
I am trying to incrementally add rows to an empty data frame. The data has 7
columns, the last 6 are numeric.

For the first columns I would like to include a text identifier, called
?Case? like Andrew, Burt, Charlie etc. that is also output to a data frame ?
this is where I am having the problem

The identifiers are being input from file which automatically assigns them
to a data frame
Code:

Inp_dat<- read.csv("/File/Input data.csv")

out_put<-data.frame(Case=character(), StdL=numeric(), StdPP=numeric(),
StdSE=numeric(), L=numeric(), MRPP=numeric(), MRSE=numeric(),
stringsAsFactors=FALSE)

for(i in 1:4)
{
if (i==1) b<-Andrew
if (i==2) b<-Burt
if (i==3) b<-Charlie
if (i==4) b<-Dave

pr <- Inp_dat$p[i]
SE_pr <- Inp_dat$SE[i]
r<- Inp_dat$r[i]
n<- Inp_dat$n[i]
Case<- levels(Inp_dat$Case)[i])
?

out_put[i,]<-data.frame(Case, stdL, stdPP, stdSE, L, PP, PP_SE)

}
out_put

  Case     StdL          StdPP           StdSE           L               
MRPP            MRSE
1    1  19.466823   0.16432300   0.03137456   26.002294   0.2080145  
0.03804692
2    1   2.334130    0.22566939   0.08962662    5.095703    0.3888451  
0.08399101
3    1   2.588678    0.05502765   0.00454159   42.058326   0.4861511  
0.02128030
4    1   7.857898    0.18457822   0.04372297    4.705487    0.1193687  
0.01921609


Unfortunately the Case column loses the labels Andrew, Burt ?

I?ve tried always to keep these, but are clearly doing something wrong. Can
anyone help?




--
View this message in context: http://r.789695.n4.nabble.com/Dataframes-and-text-identifier-columns-tp4693184.html
Sent from the R help mailing list archive at Nabble.com.
#
On Jun 29, 2014, at 5:46 AM, Brian Willis wrote:

            
# Since the question involves failure of this step, there woudn't  
appear to be much we could say unless you provided teh output of  
levels(Inp_dat$Case).
The values stdL through PP_SE seem to come from someplace else.
If you were thinking that the values Andrew and Burt would come from  
that loop then it's unclear whay tha twould be so since the value of  
"b" is never used after it is defined.
David Winsemius, MD
Alameda, CA, USA
#
Inp_dat<- read.csv("/File/Input data.csv")
out_put[i,]<-data.frame(Case, stdL, stdPP, stdSE, L, PP, PP_SE)

These two statements look like the source of the problem.
Add the optional argument
     stringsAsFactors=FALSE
to both.
Rich
On Sun, Jun 29, 2014 at 8:46 AM, Brian Willis <b.h.willis at bham.ac.uk> wrote: