Re: [R] Help with code
Hi Can you explain rules for propagating values? I do not see any pattern. Only when there is no Y in a line you want to fill all columns with either T1D_noc or Ctrl_noc based on t1d_ptype. I would start with narrowing the levels in last column as it seems to me there is no difference between Ctrl and Ctrl_FDR in desired result levels(c1$t1d_ptype)[1:2] <- "Ctrl" If you want to retain old values just make a new column with t1d_ptype and change levels only in this new column. select all rows without Y selection<-which(rowSums(c1[,1:6]=="")==6) change columns 1:6 to character instead of factors c1[,1:6]<-sapply(c2[,1:6], as.character) put values from last column to other columns and add "noc" c1[selection,1:6]<-paste(c1[selection,7], "noc", sep="_") and after that I am lost. Petr > > structure(list(HTN = 1:10, HTN_FDR = structure(c(4L, 2L, 1L, > 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc", "T1d_w"), class = "factor"), Dyslipidemia = structure(c(3L, > 2L, 1L, 2L, 4L, 1L, 1L, 2L, 4L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc", "T1D_w"), class = "factor"), CAD = structure(c(3L, > 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc"), class = "factor"), CAD_FDR = structure(c(3L, 2L, 1L, > 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc"), class = "factor"), Prior_MI = structure(c(3L, 2L, > 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc"), class = "factor"), t1d_ptype = structure(c(3L, 3L, > 2L, 3L, 1L, 1L, 2L, 3L, 3L, 3L), .Label = c("Ctrl", "Ctrl_FDR", > "T1D"), class = "factor")), .Names = c("HTN", "HTN_FDR", "Dyslipidemia", > "CAD", "CAD_FDR", "Prior_MI", "t1d_ptype"), class = "data.frame", row.names > = c(NA, > -10L)) > > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with- > code-tp4218989p4228759.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. __ R-help@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.
Re: [R] Help with code
Hello, There are so many people posting answers that I'm curious and decided to try one. I don't know if this is it but it doesn't give an error and it reformats your data according to the rules in your original code. # nr <- dim(c1)[1] nc <- dim(c1)[2] c2 <- NULL c2_row <- rep("", nc-1) for(i in 1:nr){ ptype<- as.character(c1$t1d_ptype[i]) stype<- substr(ptype,1,4) num_comp <- sum(c1[i,] == "Y") for(j in 1:(nc-1)){ c2_row[j] <- "" if(num_comp > 0){ c1_tmp <- as.integer(c1[i ,j]) if(ptype == "T1D"){ if(c1_tmp == 1) c2_row[j] <- "T1D_oc" if(c1_tmp == 2) c2_row[j] <- "T1D_w" } if(stype == "Ctrl"){ if(c1_tmp == 1) c2_row[j] <- "Ctrl_oc" if(c1_tmp == 2) c2_row[j] <- "Ctrl_w" } } else{ if(ptype == "T1D") c2_row[j] <- "T1D_noc" if(stype == "Ctrl") c2_row[j] <- "Ctrl_noc" } } c2 <- rbind(c2, c(c2_row, ptype)) } c2 <- data.frame(c2) colnames(c2) <- colnames(c1) c2 I bet there's a way to work on entire objects. This is C-like. Rui Barradas -- View this message in context: http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4229987.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with code
On Fri, Dec 23, 2011 at 9:25 AM, 1Rnwb wrote: > this is how the ouput from the code should be What code might that be? Readers of the R-help email list have no idea whatsoever what you're asking. Please include context, as requested in the posting guide. Sarah > structure(list(HTN = 1:10, HTN_FDR = structure(c(4L, 2L, 1L, > 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc", "T1d_w"), class = "factor"), Dyslipidemia = structure(c(3L, > 2L, 1L, 2L, 4L, 1L, 1L, 2L, 4L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc", "T1D_w"), class = "factor"), CAD = structure(c(3L, > 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc"), class = "factor"), CAD_FDR = structure(c(3L, 2L, 1L, > 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc"), class = "factor"), Prior_MI = structure(c(3L, 2L, > 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", > "T1D_oc"), class = "factor"), t1d_ptype = structure(c(3L, 3L, > 2L, 3L, 1L, 1L, 2L, 3L, 3L, 3L), .Label = c("Ctrl", "Ctrl_FDR", > "T1D"), class = "factor")), .Names = c("HTN", "HTN_FDR", "Dyslipidemia", > "CAD", "CAD_FDR", "Prior_MI", "t1d_ptype"), class = "data.frame", row.names > = c(NA, > -10L)) > > -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.
Re: [R] Help with code
this is how the ouput from the code should be structure(list(HTN = 1:10, HTN_FDR = structure(c(4L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", "T1D_oc", "T1d_w"), class = "factor"), Dyslipidemia = structure(c(3L, 2L, 1L, 2L, 4L, 1L, 1L, 2L, 4L, 2L), .Label = c("Ctrl_noc", "T1D_noc", "T1D_oc", "T1D_w"), class = "factor"), CAD = structure(c(3L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", "T1D_oc"), class = "factor"), CAD_FDR = structure(c(3L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", "T1D_oc"), class = "factor"), Prior_MI = structure(c(3L, 2L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L), .Label = c("Ctrl_noc", "T1D_noc", "T1D_oc"), class = "factor"), t1d_ptype = structure(c(3L, 3L, 2L, 3L, 1L, 1L, 2L, 3L, 3L, 3L), .Label = c("Ctrl", "Ctrl_FDR", "T1D"), class = "factor")), .Names = c("HTN", "HTN_FDR", "Dyslipidemia", "CAD", "CAD_FDR", "Prior_MI", "t1d_ptype"), class = "data.frame", row.names = c(NA, -10L)) -- View this message in context: http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4228759.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with code
Thanks Bill, the output of dput was very similar to your example, I was not sure so did not put it on the post. however i uploaded the foo.txt file which contains the part of the data. sharad -- View this message in context: http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4222947.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with code
here is the dump and the code once again, sorry for creating so much noise. c1<-structure(list(HTN = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), HTN_FDR = structure(c(2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), Dyslipidemia = structure(c(2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), CAD = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), CAD_FDR = structure(c(2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), Prior_MI = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("", "Y"), class = "factor"), t1d_ptype = structure(c(3L, 3L, 2L, 3L, 1L, 1L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 3L, 2L, 2L, 1L, 1L, 3L), .Label = c("Ctrl", "Ctrl_FDR", "T1D"), class = "factor")), .Names = c("HTN", "HTN_FDR", "Dyslipidemia", "CAD", "CAD_FDR", "Prior_MI", "t1d_ptype" ), row.names = c(NA, 25L), class = "data.frame") c2<-c1 any_comp<-NULL 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" } } -- View this message in context: http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4222965.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with code
Hi > > I do not know how to use dput, i am attaching the txt file for the data dput(any.object) puts a structure of this object to console. You can copy it to your email and anybody can copy it back to R. Or you can transfer the structure to file see ?dput, ?dget > http://r.789695.n4.nabble.com/file/n4222616/foo.txt foo.txt > > c1<-read.dlim('foo.txt') > c2<-c1 > > any_comp<-NULL > > 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 ^^ this line is never true. There is no 2 in a c1. Maybe you shall really tell us what is your intention with some simple example. Here is a subset of your file c1 my.c1 <- structure(list(HTN = structure(c(2L, 1L, 1L), .Label = c("", "Y"), class = "factor"), HTN_FDR = structure(c(2L, 2L, 2L), .Label = c("", "Y"), class = "factor"), Dyslipidemia = structure(c(2L, 1L, 2L ), .Label = c("", "Y"), class = "factor"), CAD = c(NA, NA, NA ), CAD_FDR = structure(c(2L, 2L, 1L), .Label = c("", "Y"), class = "factor"), Prior_MI = c(NA, NA, NA)), .Names = c("HTN", "HTN_FDR", "Dyslipidemia", "CAD", "CAD_FDR", "Prior_MI"), row.names = c("1", "5", "9"), class = "data.frame") Forget your complicated code and tell us what is you desired output? Regards Petr > 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" >} > } > > this gives me whether the particular t1d_ptype has a specific complication > as well as there is another complication as well. > > I will appreciate help very much. > thanks > sharad > > -- > View this message in context: http://r.789695.n4.nabble.com/Help-with- > code-tp4218989p4222616.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. __ R-help@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.
Re: [R] Help with code
To make it so others can produce your dataset on their own computers (so they can easily reproduce the problem you are having) do, in R, dump("c1", file=stdout()) and copy the text output by that that into your message. E.g., suppose I made a data.frame with junk <- data.frame(x=runif(4),y=runif(4)) and forgot to save the random seed before making it. Instead of sending the calls to runif I could use dump(): > dump("junk", file=stdout()) junk <- structure(list(x = c(0.415693838847801, 0.399903971236199, 0.999439711682498, 0.331607921980321), y = c(0.827737988438457, 0.193867813330144, 0.0372913987375796, 0.62276106630452)), .Names = c("x", "y"), row.names = c(NA, -4L), class = "data.frame") and would copy all of its output into the mail message (there is no need to copy the dump() command itself). It isn't pretty but it gets the job done without any fuss. Some mailers like to break lines that they consider too long and that can change the meaning of the code produced by dump(). If your mailer does that, use dump("c1", file="c1.txt") # or "c:/temp/c1.txt", etc. and attach the file c1.txt to your mail message. We will see if my mailer breaks up the lines in my example. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of 1Rnwb > Sent: Wednesday, December 21, 2011 10:07 AM > To: r-help@r-project.org > Subject: Re: [R] Help with code > > I do not know how to use dput, i am attaching the txt file for the data > http://r.789695.n4.nabble.com/file/n4222616/foo.txt foo.txt > > c1<-read.dlim('foo.txt') > c2<-c1 > > any_comp<-NULL > > 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" >} > } > > this gives me whether the particular t1d_ptype has a specific complication > as well as there is another complication as well. > > I will appreciate help very much. > thanks > sharad > > -- > View this message in context: > http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4222616.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. __ R-help@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.
Re: [R] Help with code
I do not know how to use dput, i am attaching the txt file for the data http://r.789695.n4.nabble.com/file/n4222616/foo.txt foo.txt c1<-read.dlim('foo.txt') c2<-c1 any_comp<-NULL 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" } } this gives me whether the particular t1d_ptype has a specific complication as well as there is another complication as well. I will appreciate help very much. thanks sharad -- View this message in context: http://r.789695.n4.nabble.com/Help-with-code-tp4218989p4222616.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] Help with code
Hi > > hello gurus, > > i have a data frame like this >HTN HTN_FDR Dyslipidemia CAD t1d_ptype[1:25] > 1Y YY T1D > 2 T1D > 3 Ctrl_FDR > 4 T1D > 5Y Ctrl > 6 Ctrl > 7 Ctrl_FDR > 8 T1D > 9YY T1D > 10 T1D > 11 Ctrl_FDR > 12 YY T1D > 13 Y YY 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. To do what? The only error i get is Error: object 'c1' not found You probably named your data frame c1 but I do not see any numbers in c1 With your programming style you shall probably use C+ or similar. In R it is almost always better to operate on whole objects. Without knowing what you want to achive and without any data I can only suggest to look at ?factor ?interaction Regards Petr > 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@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. __ R-help@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.
Re: [R] Help with code
Fair enough and good point. How about, dangerous when used unknowingly! On Tue, Dec 20, 2011 at 1:01 PM, William Dunlap wrote: > 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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] > On Behalf Of Justin Haynes > > Sent: Tuesday, December 20, 2011 11:54 AM > > To: 1Rnwb > > Cc: r-help@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 wrote: > > > > > hello gurus, > > > > > > i have a data frame like this > > > HTN HTN_FDR Dyslipidemia CAD t1d_ptype[1:25] > > > 1Y YY T1D > > > 2 T1D > > > 3 Ctrl_FDR > > > 4 T1D > > > 5Y Ctrl > > > 6 Ctrl > > > 7 Ctrl_FDR > > > 8 T1D > > > 9YY T1D > > > 10 T1D > > > 11 Ctrl_FDR > > > 12 YY T1D > > > 13 Y YY 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]<-"Ctr
Re: [R] 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-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Justin Haynes > Sent: Tuesday, December 20, 2011 11:54 AM > To: 1Rnwb > Cc: r-help@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 wrote: > > > hello gurus, > > > > i have a data frame like this > > HTN HTN_FDR Dyslipidemia CAD t1d_ptype[1:25] > > 1Y YY T1D > > 2 T1D > > 3 Ctrl_FDR > > 4 T1D > > 5Y Ctrl > > 6 Ctrl > > 7 Ctrl_FDR > > 8 T1D > > 9YY T1D > > 10 T1D > > 11 Ctrl_FDR > > 12 YY T1D > > 13 Y YY 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
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 wrote: > hello gurus, > > i have a data frame like this > HTN HTN_FDR Dyslipidemia CAD t1d_ptype[1:25] > 1Y YY T1D > 2 T1D > 3 Ctrl_FDR > 4 T1D > 5Y Ctrl > 6 Ctrl > 7 Ctrl_FDR > 8 T1D > 9YY T1D > 10 T1D > 11 Ctrl_FDR > 12 YY T1D > 13 Y YY 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@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@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.
Re: [R] help with Code
Hi, 1) Still not reproducible because we do not have "E.csv" 2) The help list is not for homework questions. Many (most?) professors and/or their TAs offer office hours where they are willing to provide help and guidance (unless, of course, the assignment is meant to encourage you to learn on your own rather than relying on canned solutions) 3) try something like this to track down where the error is: ## This will enable debugging so you can watch the code as each line is executed debug(chart.Regression) ## Now when you run this, a browser will come up for you to examine the code as it is run chart.Regression(MCD,SPX, Rf = .03/12, excess.returns = TRUE, main = "Security Characteristic Line", fit = c("loess", "linear"), legend.loc = "topleft") Also, right after the error occurs, you can run: traceback() to print the call stack leading to the error. This can be useful information and may be enough for you to pin the problem down, but if the real problem occurred a while back, but no errors occurred until later, traceback() may not be sufficient and you'll need to debug. I would read all the documentation pages for ?debug ?browser ?traceback if you have never used them before prior to starting this endeavour. Those tools will definitely get you to the exact point the error occurs. Good luck and do treat education as an opportunity to develop useful skills, not just a way to get some letter grades on a piece of paper. Cheers, Josh On Sun, Jul 10, 2011 at 2:03 PM, finguy wrote: > R version 2.13.0 (2011-04-13) > Copyright (C) 2011 The R Foundation for Statistical Computing > ISBN 3-900051-07-0 > Platform: i386-pc-mingw32/i386 (32-bit) > > Thanks for responding Josh. I got all the codes from my professor. Here is > what was required > > > ##Load fPortfolio, PerformanceAnalytics, fOptions and all other components > associated with these programs > > #Load Data (CSV File) > Data = read.table(file="E.csv",header=T,sep=",",row.names="Date") > Data = readSeries(file="E.csv",header=T,sep=",",format="%Y-%m-%d") > options(max.print=5.5E5) > setRmetricsOptions(max.print="5.5E5") > > #Label Data Parameters > SPX = Data[, c("SPX")] > Riskfree = Data[, c("USGG10yr")] > MCD = Data[, c("MCD")] > BA = Data[, c("GE")] > MSFT = Data[, c("MSFT")] > CVX = Data[, c("CVX")] > DIS = Data[, c("DIS")] > CORP = Data[, c("196298GR8")] > GOVT = Data[, c("000369CA")] > HY = Data[, c("812026BA")] > CPI = Data[, c("CPIINDX")] > JOB = Data[, c("USMMMNCH")] > CC = Data[, c("CONSSENT")] > SPX USGG10yr MCD GE MSFT CVX DIS 000369CA 196298GR8 > 812026BA CPIINDX USMMMNCH CONSSENT > ##Summary of Statisitics > table.Stats(Data[,1:13]) > t(table.Stats(Data)) > result=t(table.Stats(Data)) > require("Hmisc") > textplot(format.df(result, na.blank=TRUE, numeric.dollar=FALSE, > cdec=c(rep(1,2),rep(3,14))), rmar = 0.8, cmar = 1.5, max.cex=.9, halign = > "center", valign = "top", row.valign="center", wrap.rownames=10, > wrap.colnames=10, mar = c(0,0,3,0)+0.1) > title(main="Statistics for FIN 355 Project Data") > > #Correlation Matrix with Distribution and SCL > chart.Correlation(Data, histogram=TRUE, pch="+") > chart.Correlation(Data[,1:10], histogram=TRUE, pch="+") > > # CAPM, Efficient Frontier > PData = Data[,3:10] > Spec = portfolioSpec() > setTargetReturn(Spec) = mean(colMeans(PData)) > Constraints = "LongOnly" > efficientPortfolio(PData, Spec, Constraints) > Frontier = portfolioFrontier(PData) > frontierPlot(Frontier, col = c("orange", "orange"), pch = 19) > minvarport = minvariancePoints(Frontier, pch = 19, col = "red") > minvariancePortfolio(PData) > cmlp = tangencyPoints(Frontier, pch = 19, col = "blue") > cml = tangencyLines(Frontier, col = "blue") > tangencyPortfolio(PData) > ew = equalWeightsPoints(Frontier, pch = 15, col = "green") > sap = singleAssetPoints(Frontier, pch = 25, cex = 2.0, col = topo.colors(8)) > > #Single Factor Index Model - Regression > > BetaCoVariance(MCD,SPX) > BetaCoVariance(GE,SPX) > BetaCoVariance(MSFT,SPX) > BetaCoVariance(CVX,SPX) > BetaCoVariance(DIS,SPX) > > **EXTRA CREDIT** > Frontier, time series, time date > ##Securities Characteristic Line > chart.Regression(MCD,SPX, Rf = .03/12, excess.returns = TRUE, main = > "Security Characteristic Line", fit = c("loess", "linear"), legend.loc = > "topleft") > > Everything works until i get to the securities Characteristic Line. I get > the following: > >> chart.Regression(MCD,SPX, Rf = .03/12, excess.returns = TRUE, main = >> "Security Characteristic Line", fit = c("loess", "linear"), legend.loc = >> "topleft") > Error in as.vector(data[, i]) : subscript out of bounds > > I don't know much about R but I'm hoping somebody can find the error in the > code or provide the package I need. Thanks > > -- > View this message in context: > http://r.789695.n4.nabble.com/help-with-Code-tp3658134p3658242.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@r-project.org mailing
Re: [R] help with Code
R version 2.13.0 (2011-04-13) Copyright (C) 2011 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i386-pc-mingw32/i386 (32-bit) Thanks for responding Josh. I got all the codes from my professor. Here is what was required ##Load fPortfolio, PerformanceAnalytics, fOptions and all other components associated with these programs #Load Data (CSV File) Data = read.table(file="E.csv",header=T,sep=",",row.names="Date") Data = readSeries(file="E.csv",header=T,sep=",",format="%Y-%m-%d") options(max.print=5.5E5) setRmetricsOptions(max.print="5.5E5") #Label Data Parameters SPX = Data[, c("SPX")] Riskfree = Data[, c("USGG10yr")] MCD = Data[, c("MCD")] BA = Data[, c("GE")] MSFT = Data[, c("MSFT")] CVX = Data[, c("CVX")] DIS = Data[, c("DIS")] CORP = Data[, c("196298GR8")] GOVT = Data[, c("000369CA")] HY = Data[, c("812026BA")] CPI = Data[, c("CPIINDX")] JOB = Data[, c("USMMMNCH")] CC = Data[, c("CONSSENT")] SPX USGG10yr MCD GE MSFT CVX DIS 000369CA 196298GR8 812026BA CPIINDX USMMMNCH CONSSENT ##Summary of Statisitics table.Stats(Data[,1:13]) t(table.Stats(Data)) result=t(table.Stats(Data)) require("Hmisc") textplot(format.df(result, na.blank=TRUE, numeric.dollar=FALSE, cdec=c(rep(1,2),rep(3,14))), rmar = 0.8, cmar = 1.5, max.cex=.9, halign = "center", valign = "top", row.valign="center", wrap.rownames=10, wrap.colnames=10, mar = c(0,0,3,0)+0.1) title(main="Statistics for FIN 355 Project Data") #Correlation Matrix with Distribution and SCL chart.Correlation(Data, histogram=TRUE, pch="+") chart.Correlation(Data[,1:10], histogram=TRUE, pch="+") # CAPM, Efficient Frontier PData = Data[,3:10] Spec = portfolioSpec() setTargetReturn(Spec) = mean(colMeans(PData)) Constraints = "LongOnly" efficientPortfolio(PData, Spec, Constraints) Frontier = portfolioFrontier(PData) frontierPlot(Frontier, col = c("orange", "orange"), pch = 19) minvarport = minvariancePoints(Frontier, pch = 19, col = "red") minvariancePortfolio(PData) cmlp = tangencyPoints(Frontier, pch = 19, col = "blue") cml = tangencyLines(Frontier, col = "blue") tangencyPortfolio(PData) ew = equalWeightsPoints(Frontier, pch = 15, col = "green") sap = singleAssetPoints(Frontier, pch = 25, cex = 2.0, col = topo.colors(8)) #Single Factor Index Model - Regression BetaCoVariance(MCD,SPX) BetaCoVariance(GE,SPX) BetaCoVariance(MSFT,SPX) BetaCoVariance(CVX,SPX) BetaCoVariance(DIS,SPX) **EXTRA CREDIT** Frontier, time series, time date ##Securities Characteristic Line chart.Regression(MCD,SPX, Rf = .03/12, excess.returns = TRUE, main = "Security Characteristic Line", fit = c("loess", "linear"), legend.loc = "topleft") Everything works until i get to the securities Characteristic Line. I get the following: > chart.Regression(MCD,SPX, Rf = .03/12, excess.returns = TRUE, main = > "Security Characteristic Line", fit = c("loess", "linear"), legend.loc = > "topleft") Error in as.vector(data[, i]) : subscript out of bounds I don't know much about R but I'm hoping somebody can find the error in the code or provide the package I need. Thanks -- View this message in context: http://r.789695.n4.nabble.com/help-with-Code-tp3658134p3658242.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] help with Code
Hi, Please provide a *reproducible* example (as the posting guide requests). It is also courteous to report your version of R and the package which contains the chart.Regression() function. We can look it up, but it saves time if you just say it. Josh On Sun, Jul 10, 2011 at 12:52 PM, finguy wrote: > chart.Regression(MCD,SPX, Rf = .03/12, excess.returns = TRUE, main = > "Security Characteristic Line", fit = c("loess", "linear"), legend.loc = > "topleft") > Error in as.vector(data[, i]) : subscript out of bounds > > Why is this happening? Thanks > > -- > View this message in context: > http://r.789695.n4.nabble.com/help-with-Code-tp3658134p3658134.html > Sent from the R help mailing list archive at Nabble.com. > > __ > R-help@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. > -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles https://joshuawiley.com/ __ R-help@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.
Re: [R] help with code
Hi Randy, > The SAS code for what I want to do is: > /* > > if FTIStandKey=" NAH6253-003" then do; > > CoverType=" XSOP--C "; > > V_SpGrp="T"; > > V_Origin="T"; > > end; > I admit I didn't read all the sample code you added, but this is what I *think* you're trying to accomplish. mydataframe[mydataframe$FTIStandKey == "NAH6253-003", "CoverType"] <- "XSOP--C" etc. > # create a toy dataframe > mydataframe <- data.frame(A = c("var1", "var2", "var3", "var3"), B = c(10, > 11, 12, 13), C = c("y1", "y3", "y8", "y3"), stringsAsFactors = FALSE) > mydataframe A B C 1 var1 10 y1 2 var2 11 y3 3 var3 12 y8 4 var3 13 y3 > > # where A is "var3", add 10 to the value in the B column > mydataframe[mydataframe$A == "var3", "B"] <- mydataframe[mydataframe$A == > "var3", "B"] + 10 > mydataframe A B C 1 var1 10 y1 2 var2 11 y3 3 var3 22 y8 4 var3 23 y3 > > # where A is "var2", change the value in column C to "other" > mydataframe[mydataframe$A == "var2", "C"] <- "other" > mydataframe A B C 1 var1 10y1 2 var2 11 other 3 var3 22y8 4 var3 23y3 There are other ways to do the same thing, of course. I find this one easy to remember. You might want to read some documentation on subsetting. Please don't repost your question right away. Most of us are busy and will answer as soon as we can. Sarah -- Sarah Goslee http://www.functionaldiversity.org __ R-help@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.