[R] loop and sapply problem, help need

jim holtman jholtman at gmail.com
Sun Apr 10 02:38:06 CEST 2011


try this:

> x <- read.table(textConnection("
+ mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
+ 1 11 22  12 1 2 3 1
+ 2 33 44  34 1 2 3 1
+ 3 22 44  24 3 1 2 1
+ 4 11 33  13 2 2 1 3
+ 5 22 11 12 3 2 2 1 "), header = TRUE)
> closeAllConnections()
> x  # before
  mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
1    1   11   22   12   1   2   3   1
2    2   33   44   34   1   2   3   1
3    3   22   44   24   3   1   2   1
4    4   11   33   13   2   2   1   3
5    5   22   11   12   3   2   2   1
>
> # extract data to matrix for easier processing
> lcd <- as.matrix(x[grepl("^lcd", names(x))])
> # now process each 'PR' column
> for (i in grep("^PR", names(x), value = TRUE)){
+     x[[i]] <- lcd[cbind(seq(nrow(x)), x[[i]])]
+ }
> x # after
  mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
1    1   11   22   12  11  22  12  11
2    2   33   44   34  33  44  34  33
3    3   22   44   24  24  22  44  22
4    4   11   33   13  33  33  11  13
5    5   22   11   12  12  11  11  22
>


On Sat, Apr 9, 2011 at 5:27 PM, Ram H. Sharma <sharma.ram.h at gmail.com> wrote:
> Hi Phil and R users
>
> Let me clear my problem. The generating sample is not prime issue here. I am
> simulating a situation, for which I need to sample. But once I have sampling
> done, I need to do a conditional decoding. Let me clear it with more clear
> data example:
>
> x <- read.table(textConnection("
> mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
> 1 11 22  12 1 2 3 1
> 2 33 44  34 1 2 3 1
> 3 22 44  24 3 1 2 1
> 4 11 33  13 2 2 1 3
> 5 22 11 12 3 2 2 1 "), header = TRUE)
>
> I want to do a conditional formatting of PR1:PR4 (in real sense I have
>>20000 such variables) based on corrsponding value in variable column lcd1
> or lcd2 or lcd3.
>
> mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
> 1       11     22   12      1     2     3      1
> 2       33      44  34      1     2     3      1
> 3       22      44  24      3    1      2      1
> 4      11       33  13      2    2      1      3
> 5       22      11  12      3    2      2      1
>
> If PR1 =1, then PR1= lcd1
>   PR1=2, then PR1 = lcd2
>   PR1=3, then PR1 = lcd3
>     similarly for PR2...................to end of files PR4
>
> #Expected Output
> mark lcd1 lcd2 lcd3 PR1 PR2 PR3 PR4
> 1 11 22 12 11 22 12 11
> 2 33 44 34 33 44 34 33
> 3 22 44 24 24 22 44 22
> 4 11 33 13 33 33 11 13
> 5 22 11 12 12 11 11 22
>
> Thank you;
>
> Ram H
>
>
>
>
> On Sat, Apr 9, 2011 at 4:13 PM, Phil Spector <spector at stat.berkeley.edu>wrote:
>
>> Ram -
>>   I think you'll have to explain what you're trying to do.
>>   First, you're creating a 10x10 matrix newd, which could
>> be done a bit more efficiently by using
>>
>>    newd = matrix(sample(c(1,2,3,3,),10*length(pvec),replace=TRUE),ncol=10)
>>
>>   Notice that calling sapply on a matrix applies the function to each
>> element of the matrix.
>>
>>   But your fun3 function really doesn't make sense, since you are testing
>> each scalar element for equality to a scalar, and then setting
>> it equal to a vector.  It seems to me that what you're trying to do is
>>
>>
>>       smpool = c(1,2,3,3)
>>       allvals = rbind(lcd1,lcd2,lcd3)
>>       pn = replicate(10,allvals[sample(smpool,1),])
>>
>> but I can't be sure.
>>                                        - Phil Spector
>>                                         Statistical Computing Facility
>>                                         Department of Statistics
>>                                         UC Berkeley
>>                                         spector at stat.berkeley.edu
>>
>>
>>
>> On Sat, 9 Apr 2011, Ram H. Sharma wrote:
>>
>>  Dear R experts
>>>
>>> Sorry for this question
>>>
>>> M1 <- 1:10
>>> lcd1 <- c(11, 22, 33, 44, 11, 22, 33, 33, 22, 11)
>>> lcd2 <- c(22, 11, 44, 11, 33, 11, 22, 22, 11, 22)
>>> lcd3 <- c(12, 12, 34, 14, 13, 12, 23, 23, 12, 12)
>>>
>>> #generating variables through sampling
>>> pvec <- c("PR1", "PR2", "PR3", "PR4", "PR5", "PR6", "PR7", "PR8", "PR9",
>>> "PR10")
>>> fun11 <- function(x){
>>>  smpool <- c(1,2,3,3)
>>>  x <-  sample(smpool, 10, replace= TRUE)
>>>  }
>>> newd <- sapply (pvec, fun11)
>>>
>>>
>>> # function to recode this new generated data
>>> fun3 <- function(x) {
>>>      (if ( x ==1){
>>>     x = lcd1
>>>   }else  if (x ==2){
>>>       x = lcd2
>>>   }  else if ( x ==3 ){
>>>        x = lcd3
>>>       } else x = NA )
>>>               return(x)
>>>             }
>>> Applying the function:
>>> pn <- sapply (newd, fun3)
>>>
>>> I am getting 10 x 100 matrix, in contrast to 10 x 10 what I am expecting !
>>>
>>> My objective here is to replace data points in all variables in newd
>>> with corresponding value with values vector lcd1 or lcd2 or lcd3 depending
>>> upon whether they are 1 or 2 or 3.
>>> For example;
>>>
>>> lcd1 <-   c(11, 22, 33, 44, 11, 22, 33, 33, 22, 11)
>>> lcd2 <-   c(22, 11, 44, 11, 33, 11, 22, 22, 11, 22)
>>> lcd3 <-   c(12, 12, 34, 14, 13, 12, 23, 23, 12, 12)
>>>
>>> PR1 <-    c(1,   2,   3,    2,   1,  1,   1,   2, 3,   1)
>>>
>>> with the new
>>> PR1n <- c(11,  11,   34,  11,  11, 22, 33, 22, 12, 11)
>>> # as the first element of this vector is determined by PR1[1] indicator
>>> determines whether to pick lcd1[1] or lcd2[1] or lcd3[1] element
>>> similarly for PR1[2] indicator whether to pick lcd1[2] or lcd2[2] or
>>> lcd3[2]
>>> element
>>>
>>> The same process need to be continued other PR2 to PR10 variables. That's
>>> why I attempted to use sapply.
>>>
>>> Thank you for your help
>>>
>>>
>>> --
>>>
>>> Ram H
>>>
>>>        [[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.
>>>
>>>
>
>
> --
>
> Ram H
>
>        [[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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list