Skip to content

Help with code

14 messages · 1Rnwb, William Dunlap, Justin Haynes +3 more

#
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.
#
Re
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
#
Hi
c2[i,j]<-"T1D_oc"
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
http://www.R-project.org/posting-guide.html
#
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.
#
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():
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
#
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.
#
Hi
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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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
c2[i,j]<-"T1D_w"
c2[i,j]<-"T1D_oc"
c2[i,j]<-"Ctrl_noc"
complication
http://www.R-project.org/posting-guide.html
1 day later
#
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.
#
On Fri, Dec 23, 2011 at 9:25 AM, 1Rnwb <sbpurohit at gmail.com> wrote:
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

  
    
#
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.
3 days later
#
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
row.names
http://www.R-project.org/posting-guide.html