Help with code
Re
your column (i think its called t1d_ptype[1:25]) is a factor and using factors is dangerous at best.
This depends on how you want to define "dangerous". If t1d_ptype ought
take values from a certain set of strings then making it a factor gives
you some safety, since it warns you when you go outside of that set and
try to give it an illegal value. E.g.,
> sex <- factor(c("M","F","F"), levels=c("F", "M"))
> sex[2] <- "no"
Warning message:
In `[<-.factor`(`*tmp*`, 2, value = "no") :
invalid factor level, NAs generated
It does take more work to set up, since you need to enumerate the set
of good strings. That is tedium, not danger.
If t1d_ptype might take any value, then make it a character vector.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Justin Haynes Sent: Tuesday, December 20, 2011 11:54 AM To: 1Rnwb Cc: r-help at r-project.org Subject: Re: [R] Help with code the short answer... which is a guess cause you didn't provide a reproducible example... is: your column (i think its called t1d_ptype[1:25]) is a factor and using factors is dangerous at best. you can check with ?str. see ?factor for how to convert back to strings and see if your code works. to answer your second question, yes I'm sure there is a better simple way to do this, but i can't follow what you're doing... for example, I don't know what c1 is... but, the place I would look is at the plyr package. its excellent at splitting and reordering data. and one final note, you should avoid naming things with pre-existing R functions (e.g. data). Justin On Tue, Dec 20, 2011 at 11:14 AM, 1Rnwb <sbpurohit at gmail.com> wrote:
hello gurus,
i have a data frame like this
HTN HTN_FDR Dyslipidemia CAD t1d_ptype[1:25]
1 Y Y Y T1D
2 T1D
3 Ctrl_FDR
4 T1D
5 Y Ctrl
6 Ctrl
7 Ctrl_FDR
8 T1D
9 Y Y T1D
10 T1D
11 Ctrl_FDR
12 Y Y T1D
13 Y Y Y T1D
14 T1D
15 Ctrl
16 Ctrl
17 Ctrl_FDR
18 T1D
19 T1D
20 Y T1D
21 Ctrl_FDR
22 Ctrl_FDR
23 Ctrl
24 Ctrl
25 T1D
i am converting it to define the groups more uniformly using this code:
for( i in 1:dim(c1)[1])
{
num_comp<-0
for (j in 1:dim(c1)[2])
if (c1[i,j]==2) num_comp=num_comp+1 #"Y"=2
for (j in 1:dim(c1)[2])
if(num_comp>0)
{
if (data$t1d_ptype[i] == "T1D" && c1[i ,j] == 2) c2[i,j]<-"T1D_w"
if (data$t1d_ptype[i] == "T1D" && c1[i, j] == 1) c2[i,j]<-"T1D_oc"
if(substr(data$t1d_ptype[i],1,4) == "Ctrl" && c1[i,j] == 2)
c2[i,j]<-"Ctrl_w"
if (substr(data$t1d_ptype[i],1,4) == "Ctrl" && c1[i,j] == 1)
c2[i,j]<-"Ctrl_oc"
}
else
{
if(data$t1d_ptype[i] == "T1D") c2[i,j]<-"T1D_noc"
if(substr(data$t1d_ptype[i],1,4) == "Ctrl") c2[i,j]<-"Ctrl_noc"
}
}
it is giving me error
In `[<-.factor`(`*tmp*`, iseq, value = structure(c(NA, ... :
invalid factor level, NAs generated
Also it there a simple way to do this.
Thanks
Sharad
--
View this message in context:
http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4218989.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________ R-help at 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.
[[alternative HTML version deleted]]
______________________________________________ R-help at 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.