Re: [R] remove rows with infinite/nan values from a zoo dataset

2013-09-02 Thread arun
Hi,
Please dput() the example dataset.  When I read from the one shown below, it 
looks a bit altered.

library(zoo)
dat1<- read.zoo(text="2009-07-15,#N/A N/A,#N/A N/A,18.96858
2009-07-16,20.30685,20.40664,#N/A N/A
2009-07-17,20.78813,20.03991,20.40664
2009-07-20,21.41278,21.41278,20.03991
2009-07-21,22.9963,22.98397,21.41278
2009-07-22,23.06443,23.01112,22.98397
2009-07-23,23.45905,24.72232,23.01112
2009-07-24,24.89291,25.56603,24.72232
2009-07-27,25.38929,24.80535,25.56603
2009-07-28,25.26712,25.65566,24.80535
2009-07-29,25.83884,24.98163,25.65566
2009-07-30,#N/A N/A,#N/A N/A,24.98163
2009-08-03,25.25553,25.93297,#N/A N/A
2009-08-04,26.02464,25.49159,25.93297
",sep=",",header=FALSE,FUN=as.Date,format="%Y-%m-%d",fill=TRUE) 


dput(dat1)  ###
structure(c(NA, 20.30685, 20.78813, 21.41278, 22.9963, 23.06443, 
23.45905, 24.89291, 25.38929, 25.26712, 25.83884, NA, 25.25553, 
26.02464, NA, 20.40664, 20.03991, 21.41278, 22.98397, 23.01112, 
24.72232, 25.56603, 24.80535, 25.65566, 24.98163, NA, 25.93297, 
25.49159, NA, NA, 20.40664, 20.03991, 21.41278, 22.98397, 23.01112, 
24.72232, 25.56603, 24.80535, 25.65566, NA, NA, 25.93297), .Dim = c(14L, 
3L), .Dimnames = list(NULL, c("V2", "V3", "V4")), index = structure(c(14440, 
14441, 14442, 14445, 14446, 14447, 14448, 14449, 14452, 14453, 
14454, 14455, 14459, 14460), class = "Date"), class = "zoo")


dat2<- dat1[!rowSums(is.na(dat1)),]
dat2
# V2   V3   V4
#2009-07-17 20.78813 20.03991 20.40664
#2009-07-20 21.41278 21.41278 20.03991
#2009-07-21 22.99630 22.98397 21.41278
#2009-07-22 23.06443 23.01112 22.98397
#2009-07-23 23.45905 24.72232 23.01112
#2009-07-24 24.89291 25.56603 24.72232
#2009-07-27 25.38929 24.80535 25.56603
#2009-07-28 25.26712 25.65566 24.80535
#2009-07-29 25.83884 24.98163 25.65566
#2009-08-04 26.02464 25.49159 25.93297


dat2[1,2]<- Inf
 dat2[5,3]<- -Inf


dat2[rowSums(is.finite(dat2))==ncol(dat2),]
# V2   V3   V4
#2009-07-20 21.41278 21.41278 20.03991
#2009-07-21 22.99630 22.98397 21.41278
#2009-07-22 23.06443 23.01112 22.98397
#2009-07-24 24.89291 25.56603 24.72232
#2009-07-27 25.38929 24.80535 25.56603
#2009-07-28 25.26712 25.65566 24.80535
#2009-07-29 25.83884 24.98163 25.65566
#2009-08-04 26.02464 25.49159 25.93297


A.K.

Hi There, 

I have a dataset with many rows and few columns as following: 

2009-07-15  #N/A N/A#N/A N/A18.96858 
2009-07-16  20.3068520.40664#N/A N/A 
2009-07-17  20.7881320.0399120.40664 
2009-07-20  21.4127821.4127820.03991 
2009-07-21  22.9963 22.9839721.41278 
2009-07-22  23.0644323.0111222.98397 
2009-07-23  23.4590524.7223223.01112 
2009-07-24  24.8929125.5660324.72232 
2009-07-27  25.3892924.8053525.56603 
2009-07-28  25.2671225.6556624.80535 
2009-07-29  25.8388424.9816325.65566 
2009-07-30  #N/A N/A#N/A N/A24.98163 
2009-08-03  25.2555325.93297#N/A N/A 
2009-08-04  26.0246425.4915925.93297 

The class of the dataset is "zoo". My question might be stupid 
but could anyone suggest a way to remove the rows with #N/A values? 
I tried "rapply" command but it didn't work due to the data class. 

btw, how about for the "Inf" values? 

Thank you in advance!

__
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] Question about the prediction plot in pls package

2013-09-02 Thread Bjørn-Helge Mevik
Euna Jeong  writes:

> R> plot(gas1, ncomp=2, asp = 1, line = TRUE)
>
> This shows only the cross-validated predictions.

If you add the argument which = c("train", "validation") (see
?predplot.mvr), you will get both.  However, you will get them in
separate panels in the plot.

If you wish to have them in the same panel, you will have to add the
points yourself.  This should work:

plot(gas1, ncomp=2, asp = 1, line = TRUE)
points(predict(gas1, ncomp = 2) ~ gasoline$octane, col = "red")

-- 
Regards,
Bjørn-Helge Mevik

__
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] R dataframe and looping help

2013-09-02 Thread arun
HI,
Try:
res<- lapply(seq_len(ncol(dat2)),function(i)
{
x1<-cbind(dat1New[,c(1:4)],dat2[,i]);
colnames(x1)[5]<- colnames(dat2)[i];
x2<-x1[x1[,5]!=0,];
x2$previoustripstore<-ave(x2$Store,x2$CUSTID,FUN=function(x) 
c("",x[-length(x)]));
x2$Nexttripstore<- ave(x2$Store,x2$PANID,FUN=function(x) c(x[-1],""))
x2
}
) 


In my previous reply, I used ?within().  


A.K.




Hi Arun.. I made the factor to character and eventually date conversion. 

I am able to see the res dataframe, but it only has the store 
names in it.. I cant see all columns like your output? Here is code i 
use.. 

Output is name of my dataframe and 1st 4 columns are 
CustID,TripID,TripDate,Store 

# CONVERT all factor to character type 
Output[]<-lapply(Output,function(x) if(is.factor(x)) as.character(x) else x) 
# SORT 
dat1New<-Output[order(Output$CUSTID,as.Date(Output$TRIPDATE,"%m-%d-%y"),Output$Store),]
 

dat2<- dat1New[,-c(1:4)] 

res<- lapply(seq_len(ncol(dat2)),function(i) 
{ 
x1<-cbind(dat1New[,c(1:4)],dat2[,i]); 
colnames(x1)[5]<- colnames(dat2)[i]; 
x2<-x1[x1[,5]!=0,]; 
previoustripstore<-ave(x2$Store,x2$CUSTID,FUN=function(x) c("",x[-length(x)])); 
Nexttripstore<- ave(x2$Store,x2$PANID,FUN=function(x) c(x[-1],"")) 
} 
) 

What am i doing wrong? 


- Original Message -
From: arun 
To: R help 
Cc: 
Sent: Monday, September 2, 2013 11:29 PM
Subject: Re: R dataframe and looping help

HI Satish,

colnames(Output)[4]<- colnames(dat2)[i]; #guess this line should be:

colnames(x1)[4]<- colnames(dat2)[i]

Regarding the warning, I used 

read.table(..., stringsAsFactors=FALSE).  In your case, you might need to 
either use that option while reading the data or convert the factor variables 
to character class.

Check:
str(Output) 


I forgot about sorting the data.  You can use either ?sort() or ?order

 
dat1New<-dat1[order(dat1$CustID,as.Date(dat1$TripDate,"%d-%b-%y"),dat1$Store),] 
 #in the example data, it didn't change anything

dat2<- dat1New[,-c(1:3)]
str(dat1New)
'data.frame':    7 obs. of  7 variables:
 $ CustID  : int  1 1 1 1 2 2 2
 $ TripDate: chr  "2-Jan-12" "6-Jan-12" "9-Jan-12" "31-Mar-13" ... ##should be 
factor in your original dataset
 $ Store   : chr  "a" "c" "a" "a" ...  #
 $ Bread   : int  2 0 3 3 0 3 3
 $ Butter  : int  0 3 3 0 3 3 0
 $ Milk    : int  2 3 0 0 3 0 0
 $ Eggs    : int  1 0 0 0 0 0 0



Suppose, I read the data with stringsAsFactors=TRUE (default is this option)

dat1<- read.table(text="
CustID TripDate Store Bread Butter Milk Eggs
1 2-Jan-12 a 2 0 2 1 
1 6-Jan-12 c 0 3 3 0 
1 9-Jan-12 a 3 3 0 0
1 31-Mar-13 a 3 0 0 0
2 31-Aug-12 a 0 3 3 0
2 24-Sep-12 a 3 3 0 0
2 25-Sep-12 b 3 0 0 0
",sep="",header=TRUE)

 str(dat1)
'data.frame':    7 obs. of  7 variables:
 $ CustID  : int  1 1 1 1 2 2 2
 $ TripDate: Factor w/ 7 levels "24-Sep-12","25-Sep-12",..: 3 6 7 5 4 1 2
 $ Store   : Factor w/ 3 levels "a","b","c": 1 3 1 1 1 1 2
 $ Bread   : int  2 0 3 3 0 3 3
 $ Butter  : int  0 3 3 0 3 3 0
 $ Milk    : int  2 3 0 0 3 0 0
 $ Eggs    : int  1 0 0 0 0 0 0


dat2<- dat1[,-c(1:3)]
 
 res<- lapply(seq_len(ncol(dat2)),function(i) 
{x1<-cbind(dat1[,c(1:3)],dat2[,i]);colnames(x1)[4]<- 
colnames(dat2)[i];x2<-x1[x1[,4]!=0,];within(x2, 
{daysbetweentrips<-unlist(tapply(as.Date(x2$TripDate,"%d-%b-%y"),list(x2$CustID),function(x)
 
c(NA,as.numeric(diff(x);previoustripstore<-ave(x2$Store,x2$CustID,FUN=function(x)
 c(NA,x[-length(x)]));Nexttripstore<- ave(x2$Store,x2$CustID,FUN=function(x) 
c(x[-1],NA))})})
Warning messages:
1: In `[<-.factor`(`*tmp*`, i, value = c(NA, 1L, 1L)) :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, i, value = c(NA, 1L)) :
  invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, i, value = c(1L, 1L, NA)) :
  invalid factor level, NA generated
---
 

To convert to character class after reading the data:
dat1[]<-lapply(dat1,function(x) if(is.factor(x)) as.character(x) else x)
 str(dat1)
#'data.frame':    7 obs. of  7 variables:
# $ CustID  : int  1 1 1 1 2 2 2
# $ TripDate: chr  "2-Jan-12" "6-Jan-12" "9-Jan-12" "31-Mar-13" ...
# $ Store   : chr  "a" "c" "a" "a" ...
# $ Bread   : int  2 0 3 3 0 3 3
# $ Butter  : int  0 3 3 0 3 3 0
# $ Milk    : int  2 3 0 0 3 0 0
# $ Eggs    : int  1 0 0 0 0 0 0


 dat2<- dat1[,-c(1:3)]
 
  res<- lapply(seq_len(ncol(dat2)),function(i) 
{x1<-cbind(dat1[,c(1:3)],dat2[,i]);colnames(x1)[4]<- 
colnames(dat2)[i];x2<-x1[x1[,4]!=0,];within(x2, 
{daysbetweentrips<-unlist(tapply(as.Date(x2$TripDate,"%d-%b-%y"),list(x2$CustID),function(x)
 
c(NA,as.numeric(diff(x);previoustripstore<-ave(x2$Store,x2$CustID,FUN=function(x)
 c(NA,x[-length(x)]));Nexttripstore<- ave(x2$Store,x2$CustID,FUN=function(x) 
c(x[-1],NA))})}) #works




A.K.


  





Hi Arun-
 
Thanks for this...
 
I ran this code. without the days between trips... Can you please 
confirm the paranthesis and code looks right.?. they do to me
 

res<- lapply(seq_len(ncol(dat2)),function(i) 
{
x1<-cbind(Output[,c(1:3)],dat2[,i]);
colnames

[R] Question about the prediction plot in pls package

2013-09-02 Thread Euna Jeong
Hi,

I'd like to draw the trained predictions and the cross-validated
predictions in the same plot to compare two predictions.

In page 3, in the pls Package paper,

R> plot(gas1, ncomp=2, asp = 1, line = TRUE)

This shows only the cross-validated predictions.

Could you tell me how to do?

Thank you in advance!

[[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] R dataframe and looping help

2013-09-02 Thread arun
HI Satish,

colnames(Output)[4]<- colnames(dat2)[i]; #guess this line should be:

colnames(x1)[4]<- colnames(dat2)[i]

Regarding the warning, I used 

read.table(..., stringsAsFactors=FALSE).  In your case, you might need to 
either use that option while reading the data or convert the factor variables 
to character class.

Check:
str(Output) 


I forgot about sorting the data.  You can use either ?sort() or ?order

 
dat1New<-dat1[order(dat1$CustID,as.Date(dat1$TripDate,"%d-%b-%y"),dat1$Store),] 
 #in the example data, it didn't change anything

dat2<- dat1New[,-c(1:3)]
str(dat1New)
'data.frame':    7 obs. of  7 variables:
 $ CustID  : int  1 1 1 1 2 2 2
 $ TripDate: chr  "2-Jan-12" "6-Jan-12" "9-Jan-12" "31-Mar-13" ... ##should be 
factor in your original dataset
 $ Store   : chr  "a" "c" "a" "a" ...  #
 $ Bread   : int  2 0 3 3 0 3 3
 $ Butter  : int  0 3 3 0 3 3 0
 $ Milk    : int  2 3 0 0 3 0 0
 $ Eggs    : int  1 0 0 0 0 0 0



Suppose, I read the data with stringsAsFactors=TRUE (default is this option)

dat1<- read.table(text="
CustID TripDate Store Bread Butter Milk Eggs
1 2-Jan-12 a 2 0 2 1 
1 6-Jan-12 c 0 3 3 0 
1 9-Jan-12 a 3 3 0 0
1 31-Mar-13 a 3 0 0 0
2 31-Aug-12 a 0 3 3 0
2 24-Sep-12 a 3 3 0 0
2 25-Sep-12 b 3 0 0 0
",sep="",header=TRUE)

 str(dat1)
'data.frame':    7 obs. of  7 variables:
 $ CustID  : int  1 1 1 1 2 2 2
 $ TripDate: Factor w/ 7 levels "24-Sep-12","25-Sep-12",..: 3 6 7 5 4 1 2
 $ Store   : Factor w/ 3 levels "a","b","c": 1 3 1 1 1 1 2
 $ Bread   : int  2 0 3 3 0 3 3
 $ Butter  : int  0 3 3 0 3 3 0
 $ Milk    : int  2 3 0 0 3 0 0
 $ Eggs    : int  1 0 0 0 0 0 0


dat2<- dat1[,-c(1:3)]
 
 res<- lapply(seq_len(ncol(dat2)),function(i) 
{x1<-cbind(dat1[,c(1:3)],dat2[,i]);colnames(x1)[4]<- 
colnames(dat2)[i];x2<-x1[x1[,4]!=0,];within(x2, 
{daysbetweentrips<-unlist(tapply(as.Date(x2$TripDate,"%d-%b-%y"),list(x2$CustID),function(x)
 
c(NA,as.numeric(diff(x);previoustripstore<-ave(x2$Store,x2$CustID,FUN=function(x)
 c(NA,x[-length(x)]));Nexttripstore<- ave(x2$Store,x2$CustID,FUN=function(x) 
c(x[-1],NA))})})
Warning messages:
1: In `[<-.factor`(`*tmp*`, i, value = c(NA, 1L, 1L)) :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, i, value = c(NA, 1L)) :
  invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, i, value = c(1L, 1L, NA)) :
  invalid factor level, NA generated
---
 

To convert to character class after reading the data:
dat1[]<-lapply(dat1,function(x) if(is.factor(x)) as.character(x) else x)
 str(dat1)
#'data.frame':    7 obs. of  7 variables:
# $ CustID  : int  1 1 1 1 2 2 2
# $ TripDate: chr  "2-Jan-12" "6-Jan-12" "9-Jan-12" "31-Mar-13" ...
# $ Store   : chr  "a" "c" "a" "a" ...
# $ Bread   : int  2 0 3 3 0 3 3
# $ Butter  : int  0 3 3 0 3 3 0
# $ Milk    : int  2 3 0 0 3 0 0
# $ Eggs    : int  1 0 0 0 0 0 0


 dat2<- dat1[,-c(1:3)]
 
  res<- lapply(seq_len(ncol(dat2)),function(i) 
{x1<-cbind(dat1[,c(1:3)],dat2[,i]);colnames(x1)[4]<- 
colnames(dat2)[i];x2<-x1[x1[,4]!=0,];within(x2, 
{daysbetweentrips<-unlist(tapply(as.Date(x2$TripDate,"%d-%b-%y"),list(x2$CustID),function(x)
 
c(NA,as.numeric(diff(x);previoustripstore<-ave(x2$Store,x2$CustID,FUN=function(x)
 c(NA,x[-length(x)]));Nexttripstore<- ave(x2$Store,x2$CustID,FUN=function(x) 
c(x[-1],NA))})}) #works




A.K.


  





Hi Arun-
 
Thanks for this...
 
I ran this code. without the days between trips... Can you please 
confirm the paranthesis and code looks right.?. they do to me
 

res<- lapply(seq_len(ncol(dat2)),function(i) 
{
x1<-cbind(Output[,c(1:3)],dat2[,i]);
colnames(Output)[4]<- colnames(dat2)[i];
x2<-x1[x1[,4]!=0,];
previoustripstore<-ave(x2$store,x2$CUSTID,FUN=function(x) c(NA,x[-length(x)]));
Nexttripstore<- ave(x2$store,x2$CUSTID,FUN=function(x) c(x[-1],NA))
}
) 
 
But i get an warning like this:In `[<-.factor`(`*tmp*`, i, value = c(NA, 3L, 
3L, 3L,  ... :
  invalid factor level, NA generated
 
Wat might be wrong? Please help
 
Thanks,
Satish


- Original Message -
From: arun 
To: R help 
Cc: 
Sent: Monday, September 2, 2013 5:01 PM
Subject: Re: R dataframe and looping help

HI,
You may try this:

dat1<- read.table(text="
CustID TripDate Store Bread Butter Milk Eggs
1 2-Jan-12 a 2 0 2 1 
1 6-Jan-12 c 0 3 3 0 
1 9-Jan-12 a 3 3 0 0
1 31-Mar-13 a 3 0 0 0
2 31-Aug-12 a 0 3 3 0
2 24-Sep-12 a 3 3 0 0
2 25-Sep-12 b 3 0 0 0
",sep="",header=TRUE,stringsAsFactors=FALSE)
dat2<- dat1[,-c(1:3)]

res<- lapply(seq_len(ncol(dat2)),function(i) 
{x1<-cbind(dat1[,c(1:3)],dat2[,i]);colnames(x1)[4]<- 
colnames(dat2)[i];x2<-x1[x1[,4]!=0,];within(x2, 
{daysbetweentrips<-unlist(tapply(as.Date(x2$TripDate,"%d-%b-%y"),list(x2$CustID),function(x)
 
c(NA,as.numeric(diff(x);previoustripstore<-ave(x2$Store,x2$CustID,FUN=function(x)
 c(NA,x[-length(x)]));Nexttripstore<- ave(x2$Store,x2$CustID,FUN=function(x) 
c(x[-1],NA))})})


 res
#[[1]]
 # CustID  TripDate Store Bread Nexttripstore previoustripstore daysbetweentrips
#1  1  2-Jan-12

[R] how to calculate bioclim for table dataset in dismo package

2013-09-02 Thread Kristi Glover
Hi R Experts,
I was trying to develop model (bioclim, Domin) using a table data in 'dismo' 
package. The data I have is at table format instead of images (stack of raster 
images). I followed the procedures of  "dismo" package to calculate the bioclim 
but I could not figure it out how I can implement these procedures using table 
data. 

I have pasted an example how I did it, but it did not work. if some one has 
done it before, would you mind to suggest me how I can implement bioclim of 
'dismo' using table data?
I really appropriate your help.
Sincerely
KG


#
library(dismo)
dd<-structure(list(long = c(-75.747, -106.84, -105.27, -104.64, -103.71, 
-102.72, -101.79, -100.8, -105.69, -104.67, -103.71, -102.69, 
-101.66, -100.71, -99.685, -98.656, -97.627, -111.82, -110.2, 
-109.55, -106.88, -106.84, -105.85, -104.82, -103.84, -102.8, 
-101.83, -100.79, -99.742, -98.695, -97.744, -112.46, -111.8, 
-110.79, -108.41, -107.74), lat = c(19.792, 21.576, 21.347, 21.243, 
21.078, 21.185, 20.99, 21.067, 22.621, 22.763, 22.594, 22.704, 
22.797, 22.581, 22.642, 22.685, 22.711, 24.153, 23.44, 23.385, 
23.4, 23.702, 23.565, 23.71, 23.542, 23.654, 23.454, 23.533, 
23.596, 23.64, 23.377, 24.796, 24.459, 24.399, 24.503, 24.429
), sp1 = 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, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L), env1 = c(182.0037689, 163.3301239, 
443.0214233, 1240.155273, 1774.867432, 1909.528809, 2153.244141, 
1878.018433, 909.1315308, 1980.898438, 2271.118896, 2122.672852, 
2033.41626, 1534.658447, 828.4759522, 222.3117523, 23.4761219, 
79.52642822, 322.994751, 273.6637268, 35.5085907, 516.3795776, 
2205.419434, 2118.727539, 2178.901123, 1995.210083, 2048.075928, 
1824.84021, 1043.509888, 184.4526062, 12.6928978, 20.87172508, 
126.5344544, 258.8852844, 3.28964257, 140.3287811), env2 = c(1134L, 
550L, 2111L, 2523L, 2156L, 1209L, 1107L, 2605L, 3176L, 2490L, 
1360L, 801L, 1118L, 1484L, 2730L, 1309L, 104L, 197L, 2033L, 1339L, 
567L, 2694L, 2708L, 1806L, 1344L, 912L, 1377L, 2346L, 3265L, 
989L, 69L, 428L, 764L, 896L, 100L, 1521L), env3 = c(24.533, 24.928, 
24.707, 21.052, 21.318, 18.428, 19.041, 17.743, 24.371, 19.689, 
16.879, 16.528, 16.901, 18.015, 20.648, 25.31, 24.308, 22.528, 
22.912, 22.001, 25.097, 25.391, 19.154, 14.943, 17.143, 16.898, 
16.891, 17.563, 15.63, 24.354, 24.088, 22.527, 22.528, 22.126, 
25.317, 25.643), env4 = c(0.047969, 0.003469, 0.003385, 0.002253, 
0.000791, 0.001834, 0.008016, 0.009262, 0.003934, 0.002322, 0.00061, 
0.000799, 6.4e-05, 0, 0, 0.000107, 0.003151, 0.018915, 0.015077, 
0.004554, 0.003499, 0.002705, 0.003507, 0.001173, 0.000149, 0.000308, 
0, 0, 0, 0.00074, 0.002845, 0.017047, 0.018915, 0.017111, 0.002417, 
0.002668)), .Names = c("long", "lat", "sp1", "env1", "env2", 
"env3", "env4"), class = "data.frame", row.names = c(NA, -36L))

bioclim.model<-bioclim(dd[,4:7],dd[,3])

I got the following message
Error in function (classes, fdef, mtable)  : 
  unable to find an inherited method for function "bioclim", for signature 
"data.frame", "integer"





  
[[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] Issue with R libraries

2013-09-02 Thread Rolf Turner

On 31/08/13 22:35, prakashdevkumar wrote:

I have an Ubuntu Quantal 12.10 Server 64-bit instance. Trying to install R
libraries. Facing issue in installing library(qdap)
library(openNLP)
Can you suggest me how to go ahead.


No one should reply to you until you learn that what you are trying to 
install is a

***package*** (not a library)!!!  (A library is a collection of packages.)

cheers,

Rolf Turner

__
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] knitr: Was previously " Sweave: printing an underscore in the output from an R command"

2013-09-02 Thread Yihui Xie
On Mon, Sep 2, 2013 at 5:01 PM, David Epstein
 wrote:
> Dear Yihui
> Thanks very much for drawing my attention to knitr, which I had not heard of 
> before. Also thanks for pointing out the bug in Sweave, which I don't fully 
> understand, but I don't want to spend time and effort on understanding it. So 
> I hope you will find time to report the bug. I was pretty sure there was a 
> bug somewhere that was preventing me from doing what I wanted to do in 
> Sweave, but I misdiagnosed the source of the problem.
>
> I notice you didn't use print() or cat() in your short program for knitr. Is 
> it the case that it's necessary to use print() or cat() with \Sexpr in 
> Sweave, but unnecessary in knitr?

No, print() is superfluous; it is not necessary for either Sweave or
knitr, and cat() is a wrong way to go here, since cat() returns
character(0).

>
> I'll stick to Sweave for my current project, and try out knitr on my next 
> project. I would welcome a list of documents about knitr that I should 
> download, so as to make it as easy as possible to get started. I don't want 
> to understand the internals of knitr, but I am interested in any documents on 
> knitr, written by you or by others, directed at the user, rather than at 
> programmers of packages.

Electronic version of the documentation: http://yihui.name/knitr Paper
version: http://www.amazon.com/gp/product/1482203537

You do not need to understand the internals of knitr, otherwise I
would not mention it at all. Depending on the size and complexity of
your project, it may take you a few seconds or hours to switch from
Sweave to knitr: http://yihui.name/knitr/demo/sweave/

>
> Is it convenient to use vi(m) to produce knitr source? Can vi(m) be 
> integrated into the knitr package? My experience with editors designed 
> specially to work with particular products (like the built-in editor for 
> TeXWorks on the Mac) do not have the power of vi(m) and emacs, and I require 
> this power.

Whatever editor you use: http://yihui.name/knitr/demo/editors/

>
> @Duncan: thanks for indicating the use of cat() instead of print(). However, 
> due to the bug in Sweave pointed out by Yihui, replacing print by cat didn't 
> help me.
>
> Thanks
> David


Regards,
Yihui
--
Yihui Xie 
Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA

__
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] knitr: Was previously " Sweave: printing an underscore in the output from an R command"

2013-09-02 Thread David Epstein
Dear Yihui
Thanks very much for drawing my attention to knitr, which I had not heard of 
before. Also thanks for pointing out the bug in Sweave, which I don't fully 
understand, but I don't want to spend time and effort on understanding it. So I 
hope you will find time to report the bug. I was pretty sure there was a bug 
somewhere that was preventing me from doing what I wanted to do in Sweave, but 
I misdiagnosed the source of the problem.

I notice you didn't use print() or cat() in your short program for knitr. Is it 
the case that it's necessary to use print() or cat() with \Sexpr in Sweave, but 
unnecessary in knitr?

I'll stick to Sweave for my current project, and try out knitr on my next 
project. I would welcome a list of documents about knitr that I should 
download, so as to make it as easy as possible to get started. I don't want to 
understand the internals of knitr, but I am interested in any documents on 
knitr, written by you or by others, directed at the user, rather than at 
programmers of packages.

Is it convenient to use vi(m) to produce knitr source? Can vi(m) be integrated 
into the knitr package? My experience with editors designed specially to work 
with particular products (like the built-in editor for TeXWorks on the Mac) do 
not have the power of vi(m) and emacs, and I require this power.

@Duncan: thanks for indicating the use of cat() instead of print(). However, 
due to the bug in Sweave pointed out by Yihui, replacing print by cat didn't 
help me.

Thanks
David




On 2 Sep 2013, at 21:11, Yihui Xie wrote:

> I think Thierry meant gsub("_", "_", version$platform); he just
> typed too quickly. The point is to escape _ using \, but then people
> are often trapped in the dreams of dreams of dreams of backslashes
> like the movie Inception. And then due to a long-standing bug in
> Sweave for \Sexpr{} (sorry I forgot to report to R core), you will be
> so confused that you can never wake up and come back to the reality.
> 
> Dream level 1: when you need a backslash in a character string, you
> need "\\", which really means \; you think "\\_" should be good, but
> no --
> 
> Dream level 2: when you need one literal \ in a regular expression as
> the replacement expression, you need \\
> 
> Combine the two levels of dreams, you end up with "_".  in R
> really means \\, which really means \ in regular expressions.
> 
> Now you are good at the regular expression level, but Sweave comes and
> bites you, and that is due to this bug in the regular expression in
> Sweave Noweb syntax:
> 
>> SweaveSyntaxNoweb$docexpr
> [1] "Sexpr\\{([^\\}]*)\\}"
> 
> It should have been "Sexpr\\{([^}]*)\\}", i.e. } does not need to
> be escaped inside [], and \\ will be interpreted literally inside [].
> In your case, Sweave sees \ in \Sexpr{}, and the regular expression
> stops matching there, and is unable to see } after \, so it believes
> there is no inline R expressions in your document.
> 
> BTW, knitr does not have this bug and works well in your case:
> 
> \documentclass{article}
> \begin{document}
> \Sexpr{sub("_", "_", version$platform)}
> \end{document}
> 
> Regards,
> Yihui
> --
> Yihui Xie 
> Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
> 
> 
> On Mon, Sep 2, 2013 at 2:18 PM, David Epstein
>  wrote:
>> Dear Thierry,
>> 
>> Your suggestion doesn't work on my version of R. Here's what I get
>>> gsub("_", "\_", print(version$platform)
>> Error: '\_' is an unrecognized escape in character string starting ""\_"
>>> print(gsub("_", "\_", version$platform))
>> Error: '\_' is an unrecognized escape in character string starting ""\_"
>> 
>>> sub("_", "\\_", version$platform)
>> [1] "x86_64-apple-darwin10.8.0"
>> Sweave does not evaluate this expression when \Sexpr is applied and a tex 
>> error results
>> 
>>> sub("_", "\\\_", version$platform)
>> Error: '\_' is an unrecognized escape in character string starting ""\\\_"
>> Error message from R
>> 
>>> sub("_", "_", version$platform)
>> [1] "x86\\_64-apple-darwin10.8.0"
>> R evaluates this. However, the above examples indicate a deficiency/possible 
>> bug in the command sub, because sub does not seem to be able to output an 
>> expression with a single backslash.
>> 
>> I tried the previous version as follows in my .Rnw document
>> \Sexpr{print(sub("_", "_", version$platform))}
>> When Sweave is run, this expression is evaluated to illegal LaTeX
>> 
>> David.
>> 
>> 
>> 
>> 
>> On 2 Sep 2013, at 16:47, ONKELINX, Thierry wrote:
>> 
>>> You have to escape the underscore
>>> 
>>> \Sexpr{gsub("_", "\_", print(version$platform))}
>>> 
>>> Best regards,
>>> 
>>> Thierry
>>> 
>>> 
>>> Van: r-help-boun...@r-project.org [r-help-boun...@r-project.org] namens 
>>> David Epstein [david.epst...@warwick.ac.uk]
>>> Verzonden: maandag 2 september 2013 17:38
>>> Aan: r-help@r-project.org
>>> Onderwerp: [R] Sweave: printing an underscore

Re: [R] restructure my data

2013-09-02 Thread David Carlson
Sorry, there was a typo in my original message:

> df <- structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0), 
+   cauc = c(6462.32876712329, 1585.27397260274,
2481.67808219178, 
+   344.178082191781, 8871.57534246575, 816.780821917808, 
+   6031.33561643836, 1013.52739726027, 4913.52739726027,
+   1517.25)), .Names = c("var", "cauc"), row.names = c(NA,
10L),
+   class = "data.frame")
> datlst <- unstack(df, cauc~var)
> # or datlst <-  split(df$cauc, df$var)
> datlst
$`0`
[1] 1585.274 2481.678 8871.575 1013.527 1517.250

$`1`
[1] 6462.3288  344.1781

$`2`
[1]  816.7808 6031.3356 4913.5274

> MaxL <- max(sapply(datlst, length))
> datmat <- sapply(datlst, function(x) c(x, rep(NA,
MaxL-length(x
> datmat
0 1 2
[1,] 1585.274 6462.3288  816.7808
[2,] 2481.678  344.1781 6031.3356
[3,] 8871.575NA 4913.5274
[4,] 1013.527NANA
[5,] 1517.250NANA

David 

-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of David Carlson
Sent: Monday, September 2, 2013 4:38 PM
To: 'Wim Kreinen'; 'r-help'
Subject: Re: [R] restructure my data

Thanks for the reproducible data set. The unstack() function
produces a list of three vectors, one for each value of var, but
it cannot combine them into a matrix since the number of entries
in each is not the same. To get that you need to pad each vector
with NAs:

> df <- structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0),
cauc =
+ c(6462.32876712329,
+ 1585.27397260274, 2481.67808219178, 344.178082191781,
8871.57534246575,
+ 816.780821917808, 6031.33561643836, 1013.52739726027,
4913.52739726027,
+ 1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L),
class =
+ "data.frame")
> datlst <- unstack(dat, cauc~var)
> datlst
$`0`
[1] 1585.274 2481.678 8871.575 1013.527 1517.250

$`1`
[1] 6462.3288  344.1781

$`2`
[1]  816.7808 6031.3356 4913.5274

> MaxL <- max(sapply(datlst, length))
> datmat <- sapply(datlst, function(x) c(x, rep(NA,
MaxL-length(x
> datmat
0 1 2
[1,] 1585.274 6462.3288  816.7808
[2,] 2481.678  344.1781 6031.3356
[3,] 8871.575NA 4913.5274
[4,] 1013.527NANA
[5,] 1517.250NANA

-
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352




-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Wim Kreinen
Sent: Monday, September 2, 2013 12:07 PM
To: r-help
Subject: [R] restructure my data

My data is in this form: var has 3 conditions (0,1,2)

> df
   var  cauc
11 6462.3288
20 1585.2740
30 2481.6781
41  344.1781
50 8871.5753
62  816.7808
72 6031.3356
80 1013.5274
92 4913.5274
10   0 1517.2500

For the three conditions (0,1,2) I want the cauc-values to be
listed like
this

0 1   2
1585,2740   6462,3288 816.7808
 2481.6781  344.1781   6031.3356
...

Thanks Wim

> dput (df)
structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0), cauc =
c(6462.32876712329,
1585.27397260274, 2481.67808219178, 344.178082191781,
8871.57534246575,
816.780821917808, 6031.33561643836, 1013.52739726027,
4913.52739726027,
1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L),
class =
"data.frame")
>

[[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.

__
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] restructure my data

2013-09-02 Thread arun
Hi,
You could try:
df2<- do.call(cbind,split(df[,-1],df[,1]))

 res<-sapply(seq_len(ncol(df2)),function(i) {x<-df2[,i];x[duplicated(x)]<-NA;x})
dimnames(res)<- dimnames(df2)
res
#    0 1 2
#[1,] 1585.274 6462.3288  816.7808
#[2,] 2481.678  344.1781 6031.3356
#[3,] 8871.575    NA 4913.5274
#[4,] 1013.527    NA    NA
#[5,] 1517.250    NA    NA
A.K.




- Original Message -
From: Wim Kreinen 
To: r-help 
Cc: 
Sent: Monday, September 2, 2013 1:07 PM
Subject: [R] restructure my data

My data is in this form: var has 3 conditions (0,1,2)

> df
   var      cauc
1    1 6462.3288
2    0 1585.2740
3    0 2481.6781
4    1  344.1781
5    0 8871.5753
6    2  816.7808
7    2 6031.3356
8    0 1013.5274
9    2 4913.5274
10   0 1517.2500

For the three conditions (0,1,2) I want the cauc-values to be listed like
this

0                         1               2
1585,2740       6462,3288     816.7808
2481.6781      344.1781       6031.3356
...

Thanks Wim

> dput (df)
structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0), cauc =
c(6462.32876712329,
1585.27397260274, 2481.67808219178, 344.178082191781, 8871.57534246575,
816.780821917808, 6031.33561643836, 1013.52739726027, 4913.52739726027,
1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L), class =
"data.frame")
>

    [[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.


__
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] restructure my data

2013-09-02 Thread David Carlson
Thanks for the reproducible data set. The unstack() function
produces a list of three vectors, one for each value of var, but
it cannot combine them into a matrix since the number of entries
in each is not the same. To get that you need to pad each vector
with NAs:

> df <- structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0),
cauc =
+ c(6462.32876712329,
+ 1585.27397260274, 2481.67808219178, 344.178082191781,
8871.57534246575,
+ 816.780821917808, 6031.33561643836, 1013.52739726027,
4913.52739726027,
+ 1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L),
class =
+ "data.frame")
> datlst <- unstack(dat, cauc~var)
> datlst
$`0`
[1] 1585.274 2481.678 8871.575 1013.527 1517.250

$`1`
[1] 6462.3288  344.1781

$`2`
[1]  816.7808 6031.3356 4913.5274

> MaxL <- max(sapply(datlst, length))
> datmat <- sapply(datlst, function(x) c(x, rep(NA,
MaxL-length(x
> datmat
0 1 2
[1,] 1585.274 6462.3288  816.7808
[2,] 2481.678  344.1781 6031.3356
[3,] 8871.575NA 4913.5274
[4,] 1013.527NANA
[5,] 1517.250NANA

-
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352




-Original Message-
From: r-help-boun...@r-project.org
[mailto:r-help-boun...@r-project.org] On Behalf Of Wim Kreinen
Sent: Monday, September 2, 2013 12:07 PM
To: r-help
Subject: [R] restructure my data

My data is in this form: var has 3 conditions (0,1,2)

> df
   var  cauc
11 6462.3288
20 1585.2740
30 2481.6781
41  344.1781
50 8871.5753
62  816.7808
72 6031.3356
80 1013.5274
92 4913.5274
10   0 1517.2500

For the three conditions (0,1,2) I want the cauc-values to be
listed like
this

0 1   2
1585,2740   6462,3288 816.7808
 2481.6781  344.1781   6031.3356
...

Thanks Wim

> dput (df)
structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0), cauc =
c(6462.32876712329,
1585.27397260274, 2481.67808219178, 344.178082191781,
8871.57534246575,
816.780821917808, 6031.33561643836, 1013.52739726027,
4913.52739726027,
1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L),
class =
"data.frame")
>

[[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.

__
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] Convert chr pieces to numbers that have specific values defined by 2 vectors

2013-09-02 Thread arun
Hi,
On a bigger dataset:

#Speed:
set.seed(285)
dat1<- 
as.data.frame(matrix(paste0(sample(1:10,69*3e5,replace=TRUE),sample(LETTERS[1:10],69*3e5,replace=TRUE)),ncol=69,nrow=3e5),stringsAsFactors=FALSE)
length(unique(unlist(dat1)))
#[1] 100

set.seed(3490)
dat2<- 
data.frame(old=unique(unlist(dat1)),new=sample(1:100,100,replace=FALSE),stringsAsFactors=FALSE)
 
system.time({dat1New<-as.data.frame(array(dat2[,2][match(as.matrix(dat1),dat2[,1])],dim=
 dim(dat1),dimnames=dimnames(dat1)))})
 #user  system elapsed 
 # 1.480   0.236   1.719 

A.K.


- Original Message -
From: arun 
To: tobias schlager 
Cc: R help 
Sent: Monday, September 2, 2013 5:28 PM
Subject: Re: [R] Convert chr pieces to numbers that have specific values 
definedby 2 vectors

Hi,

You may try this:

set.seed(285)
dat1<- 
as.data.frame(matrix(paste0(sample(1:10,100,replace=TRUE),sample(LETTERS[1:10],100,replace=TRUE)),10,10),stringsAsFactors=FALSE)

set.seed(3490)
dat2<- 
data.frame(old=unique(unlist(dat1)),new=sample(1:100,63,replace=FALSE),stringsAsFactors=FALSE)
 dat1New<-as.data.frame(array(dat2[,2][match(as.matrix(dat1),dat2[,1])],dim= 
dim(dat1),dimnames=dimnames(dat1)))
dat1New
#   V1 V2 V3  V4 V5 V6 V7 V8 V9 V10
#1  68 68 68  14 48 28 30 27 17  39
#2  71  7 64  93 25 67 61 93 67  31
#3  58 27 17  37 71 31 16 51 69  19
#4  30 71 75  43 86 27 47 35 71  22
#5  22 38 59  55  6 11 10 32 54  92
#6  63 20 88  65 17 12 48 73 54  74
#7  19 61 94  99 54 83 10  7 44  49
#8   5  2 58 100 43 63 12 10 97   2
#9  63 94 91  79 95 54 57 32 94  84
#10  5 60 65  69 50 46 70 12 98  54


A.K.




- Original Message -
From: tobias schlager 
To: r-help@r-project.org
Cc: 
Sent: Monday, September 2, 2013 3:29 PM
Subject: [R] Convert chr pieces to numbers that have specific values defined    
by 2 vectors

Dear all, 

I think this is an easy task, but I don't know how to do it. Specifically, I 
have 69 columns with 300.000 rows. In each cell there is a code like 
"2E3", "4RR", etc.

I now have a list that replaces this with values, e.g., 
old    new
2E3         5
4RR        3
etc. 

The list ist about 1600 rows long, so also to extensive for normal solutions

Do you how to do that? It would be great if you could help me, 
best, 
tobebryant
__
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] Convert chr pieces to numbers that have specific values defined by 2 vectors

2013-09-02 Thread arun
Hi,

You may try this:

set.seed(285)
dat1<- 
as.data.frame(matrix(paste0(sample(1:10,100,replace=TRUE),sample(LETTERS[1:10],100,replace=TRUE)),10,10),stringsAsFactors=FALSE)

set.seed(3490)
dat2<- 
data.frame(old=unique(unlist(dat1)),new=sample(1:100,63,replace=FALSE),stringsAsFactors=FALSE)
 dat1New<-as.data.frame(array(dat2[,2][match(as.matrix(dat1),dat2[,1])],dim= 
dim(dat1),dimnames=dimnames(dat1)))
dat1New
#   V1 V2 V3  V4 V5 V6 V7 V8 V9 V10
#1  68 68 68  14 48 28 30 27 17  39
#2  71  7 64  93 25 67 61 93 67  31
#3  58 27 17  37 71 31 16 51 69  19
#4  30 71 75  43 86 27 47 35 71  22
#5  22 38 59  55  6 11 10 32 54  92
#6  63 20 88  65 17 12 48 73 54  74
#7  19 61 94  99 54 83 10  7 44  49
#8   5  2 58 100 43 63 12 10 97   2
#9  63 94 91  79 95 54 57 32 94  84
#10  5 60 65  69 50 46 70 12 98  54


A.K.




- Original Message -
From: tobias schlager 
To: r-help@r-project.org
Cc: 
Sent: Monday, September 2, 2013 3:29 PM
Subject: [R] Convert chr pieces to numbers that have specific values defined
by 2 vectors

Dear all, 

I think this is an easy task, but I don't know how to do it. Specifically, I 
have 69 columns with 300.000 rows. In each cell there is a code like 
"2E3", "4RR", etc.

I now have a list that replaces this with values, e.g., 
old    new
2E3         5
4RR        3
etc. 

The list ist about 1600 rows long, so also to extensive for normal solutions

Do you how to do that? It would be great if you could help me, 
best, 
tobebryant
__
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] Product of certain rows in a matrix

2013-09-02 Thread Bert Gunter
Gents:

The "eval(parse(...))" construction should almost always be avoided: it is
basically a misuse of R. There are exceptions, I suppose, but this does not
appear to be one of them.

Note that the use of numeric indexing does appear to be slightly faster
than logical indexing here, although I would say not enough to make a
practical difference. In any case:

A <- mat1New ## saves me a bit of editing
>
  > system.time({
+ j40<- n*seq_len(nrow(A)/n)
+ vec1<- rep("j40",n)
+ res<- eval(parse(text=
paste(paste0("A","[",paste0(vec1,"-",seq(n)-1),",]"),collapse="*")
   + ))
+ })
user  system elapsed
0.020.000.01

> system.time({
  + j <- seq_len(nrow(A))%%n
  + b <- A[j==0,]
  + for(i in seq_len(n-1))b <- b*A[j==i,]
  + })
user  system elapsed
0.250.000.27


  > system.time({
+ j <- n*seq_len(nrow(A)/n)
+ b1 <- A[j,]
+ for(i in seq_len(n-1))b1 <- b1*A[j-i,]
+ })
user  system elapsed
0.010.000.02

## One should not invest too much faith in such superficial timing tests,
however.

I will have no further comments.


Cheers,
Bert

[[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] R dataframe and looping help

2013-09-02 Thread arun
HI,
You may try this:

dat1<- read.table(text="
CustID TripDate Store Bread Butter Milk Eggs
1 2-Jan-12 a 2 0 2 1 
1 6-Jan-12 c 0 3 3 0 
1 9-Jan-12 a 3 3 0 0
1 31-Mar-13 a 3 0 0 0
2 31-Aug-12 a 0 3 3 0
2 24-Sep-12 a 3 3 0 0
2 25-Sep-12 b 3 0 0 0
",sep="",header=TRUE,stringsAsFactors=FALSE)
dat2<- dat1[,-c(1:3)]

res<- lapply(seq_len(ncol(dat2)),function(i) 
{x1<-cbind(dat1[,c(1:3)],dat2[,i]);colnames(x1)[4]<- 
colnames(dat2)[i];x2<-x1[x1[,4]!=0,];within(x2, 
{daysbetweentrips<-unlist(tapply(as.Date(x2$TripDate,"%d-%b-%y"),list(x2$CustID),function(x)
 
c(NA,as.numeric(diff(x);previoustripstore<-ave(x2$Store,x2$CustID,FUN=function(x)
 c(NA,x[-length(x)]));Nexttripstore<- ave(x2$Store,x2$CustID,FUN=function(x) 
c(x[-1],NA))})})


 res
#[[1]]
 # CustID  TripDate Store Bread Nexttripstore previoustripstore daysbetweentrips
#1  1  2-Jan-12 a 2 a     NA
#3  1  9-Jan-12 a 3 a a    7
#4  1 31-Mar-13 a 3   a  447
#6  2 24-Sep-12 a 3 b     NA
#7  2 25-Sep-12 b 3   a    1

#[[2]]
 # CustID  TripDate Store Butter Nexttripstore previoustripstore
#2  1  6-Jan-12 c  3 a  
#3  1  9-Jan-12 a  3   c
#5  2 31-Aug-12 a  3 a  
#6  2 24-Sep-12 a  3   a
 # daysbetweentrips
#2   NA
#3    3
#5   NA
#6   24

#[[3]]
 # CustID  TripDate Store Milk Nexttripstore previoustripstore daysbetweentrips
#1  1  2-Jan-12 a    2 c     NA
#2  1  6-Jan-12 c    3   a    4
#5  2 31-Aug-12 a    3       NA

#[[4]]
 # CustID TripDate Store Eggs Nexttripstore previoustripstore daysbetweentrips
#1  1 2-Jan-12 a    1       NA



A.K.


Hi, I have a very quick question.. I have a data which has sales per 
category per trip of each customer at different store locations, like 
below..(dataset1 frome xcel attachment) CustID  TripDateStore   Bread   
Butter  MilkEggs
1   2-Jan-12  a 2   0   2   1
1   6-Jan-12  c 0   3   3   0
1   9-Jan-12  a 3   3   0   0
1   31-Mar-13 a 3   0   0   0
2   31-Aug-12 a 0   3   3   0
2   24-Sep-12 a 3   3   0   0
2   25-Sep-12 b 3   0   0   0 Here i have shown 4 items and 
their sales per customer per trip at each 
store... However, my data contains around 100 columns with item names.. 
All i need to do is following: 1. Create a separate dataframe for each item. 
That is, create 100 
dataframs one for each item.. Within the dataframe for Butter, for 
example, will be contained columns 1-3 and Butter column, specifically 
filtered for rows where butter>0 in sales..(so rows 1,4,7 will be 
dropped from this dataframe)..Likewise for all items...(sample output 
for butter is: (dataset2) CustIDTripDateStore   Butter
1   6-Jan-12   c3
1   9-Jan-12   a3
2   31-Aug-12  a3
2   24-Sep-12  a3 2. In same loop, create new derived variables within 
each dataframe for 
each item... like create a lag variable for TripDate, create lag 
variable for storename in next trip, storename in previous trip etc... 
and also # days between trips to each store for each customer...(an 
example for Butter dataframe with new derived variables would be...)
Dataset needs to be sorted by CustID, TripDate, Store before creating 
derived variables (dataset3)Book1.xlsx CustID   TripDateStore   Butter  
NextTripstore previoustripstore 
daysbetweentrips
1   6-Jan-12   c3   a --
1   9-Jan-12   a3   - c-
2   31-Aug-12  a3   a --
2   24-Sep-12  a3   - a  24 Point of 
creating multiple item level dataframes is, i will use them 
iteratively as i will perform some regression on these datasets, using 
same set of variables each time

__
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] Convert chr pieces to numbers that have specific values defined by 2 vectors

2013-09-02 Thread tobias schlager
Dear all, 

I think this is an easy task, but I don't know how to do it. Specifically, I 
have 69 columns with 300.000 rows. In each cell there is a code like 
"2E3", "4RR", etc.

I now have a list that replaces this with values, e.g., 
old new
2E3 5
4RR 3
etc. 

The list ist about 1600 rows long, so also to extensive for normal solutions

Do you how to do that? It would be great if you could help me, 
best, 
tobebryant
__
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] Product of certain rows in a matrix

2013-09-02 Thread Edouard Hardy
Thank you A.K.
And do you have a solution without installing any package ?
Thank you in advance.
E.H.


Edouard Hardy


On Mon, Sep 2, 2013 at 5:56 PM, arun  wrote:

>
>
> HI,
> In my first solutions:
>  n<-3
>
>  
> t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
> #  [,1] [,2] [,3]
> #1   28   80  162
> #2  162   80   28
> #3135
>  n<-4
>
>  
> t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
> #  [,1] [,2] [,3]
> #1  252  640 1134
> #2   18   30   20
>
> A.K.
> 
> From: Edouard Hardy 
> To: arun 
> Cc: Bert Gunter ; R help 
> Sent: Monday, September 2, 2013 11:46 AM
> Subject: Re: [R] Product of certain rows in a matrix
>
>
>
> Thank you all for your responses.
> The real problem is that all your answer work for products 2 by 2.
> I now have to do the product n by n row.
> Do you have a solution ?
> Thank you in advance,
> E.H.
>
>
>
> Edouard Hardy
>
>
>
> On Mon, Sep 2, 2013 at 5:43 PM, arun  wrote:
>
> I guess in such situations,
> >
> >
> >fun1<- function(mat){
> > if(nrow(mat)%%2==0){
> > j<- 2*seq_len(nrow(mat)/2)
> > b<- mat[j,]* mat[j-1,]
> > }
> > else {mat1<- mat[-nrow(mat),]
> > j<- 2*seq_len(nrow(mat1)/2)
> > b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
> >  }
> >b
> >}
> >fun1(A)
> ># [,1] [,2] [,3]
> >
> >#[1,]4   10   18
> >#[2,]   63   64   63
> >#[3,]   18   104
> > fun1(Anew)
> ># [,1] [,2] [,3]
> >
> >#[1,]4   10   18
> >#[2,]   63   64   63
> >#[3,]   18   104
> >#[4,]135
> >
> >
> >A.K.
> >
> >
> >
> >
> >- Original Message -
> >From: arun 
> >To: Bert Gunter 
> >Cc: R help 
> >
> >Sent: Monday, September 2, 2013 11:26 AM
> >Subject: Re: [R] Product of certain rows in a matrix
> >
> >Hi Bert,
> >Thanks.  It is a better solution.
> >
> >If nrow() is not even.
> >
> >Anew<- rbind(A,c(1,3,5))
> >j<-seq_len(nrow(Anew)/2)###
> > Anew[j,]*Anew[j-1,]
> >#Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays
> >
>
> >t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
> >  [,1] [,2] [,3]
> >14   10   18
> >2   63   64   63
> >3   18   104
> >4135
> >
> >A.K.
> >
> >
> >
> >
> >
> >
> >
> >From: Bert Gunter 
> >To: arun 
> >Cc: R help 
> >Sent: Monday, September 2, 2013 10:55 AM
> >Subject: Re: [R] Product of certain rows in a matrix
> >
> >
> >
> >These elaborate manipulations are unnecessary and inefficient. Use
> indexing instead:
> >
> >j <- 2*seq_len(nrow(A)/2)
> >b <- A[j,]*A[j-1,]
> >b
> >[,1] [,2] [,3]
> >[1,]4   10   18
> >[2,]   63   64   63
> >[3,]   18   104
> >
> >[,1] [,2] [,3]
> >[1,]4   10   18
> >[2,]   63   64   63
> >[3,]   18   104
> >[,1] [,2] [,3]
> >[1,]4   10   18
> >[2,]   63   64   63
> >[3,]   18   104[,1] [,2] [,3]
> >[1,]4   10   18
> >[2,]   63   64   63
> >[3,]   18   104
> >[,1] [,2] [,3]
> >[1,]4   10   18
> >[2,]   63   64   63
> >[3,]   18   104
> >
> >
> >
> >
> >
> >On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:
> >
> >Hi,
> >>You could try:
> >>
> >>A<- matrix(unlist(read.table(text="
> >>1 2 3
> >>4 5 6
> >>7 8 9
> >>9 8 7
> >>6 5 4
> >>3 2 1
> >>",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
> >>
> >>library(matrixStats)
>
> >> res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
> >> res1
> >>#  [,1] [,2] [,3]
> >>#14   10   18
> >>#2   63   64   63
> >>#3   18   104
> >>
> >>
>
> >> res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
> >> identical(res1,res2)
> >>#[1] TRUE
> >>
> >>#or
> >> t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x)
> apply(x,2,prod)))
> >>
> >>#or
> >>library(plyr)
>
> >> as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
> >># V1 V2 V3
> >>#[1,]  4 10 18
> >>#[2,] 63 64 63
> >>#[3,] 18 10  4
> >>
> >>#or
> >>do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
> colProds(A[x,])))
> >>#or
> >>A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
> >> aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
> >>#  X1 X2 X3
> >>#1  4 10 18
> >>#2 63 64 63
> >>#3 18 10  4
> >>
> >>#or
> >>library(data.table)
> >>At<- data.table(A1,key='ID')
> >>subset(At[,lapply(.SD,colProds),by=ID],select=-1)
> >>#   X1 X2 X3
> >>#1:  4 10 18
> >>#2: 63 64 63
> >>#3: 18 10  4
> >>
> >>A.K.
> >>
> >>
> >>
> >>
> >>Hello,
> >>
> >>I have this matrix :
> >>A =
> >>1 2 3
> >>4 5 6
> >>7 8 9
> >>9 8 7
> >>6 5 4
> >>3 2 1
> >>
> >>I would like to have this matrix (product of rows 2 by 2) :
> >>A =
> >>4 10 18
> >>63 64 63
> >>18 10 4
> >>
> >>Is it possible to do that without a loop ?
> >>
> >>Thank you in advance !
> >>
> >>__
> >>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/po

[R] help

2013-09-02 Thread 董忠信








tt <- function(x) {
obrien <- function(x) {
  r <- rank(x)
  (r - 0.5)/(0.5 + length(r) - r)
}
unlist(tapply(x, riskset, obrien))
}
hi,  i am newer in R. when dealing  with a survival data, i have found the 
variable progression was not met the PH assumption.the picture show the 
residual agaist time.So i  use Cox model for time-depandent varibles.   i  use 
the default tt in function coxph,but when i use tt in 
"f<-cph(Surv(os$Stime,os$Status==1)~Metastasis+Surgery+Post.chem. 
+Age+tt(Progression)+ ALP, data=os, x=T, y=T, surv=TRUE, time.inc=60)",it 
didn't work. i don't kown what the arg"riskset" is .i beg your help . can you 
help me write down a appropriate tt expression to let me use in cph. thanks.


  Zhongxin 
Dong
  


__
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] Product of certain rows in a matrix

2013-09-02 Thread Edouard Hardy
Thank you all for your responses.
The real problem is that all your answer work for products 2 by 2.
I now have to do the product n by n row.
Do you have a solution ?
Thank you in advance,
E.H.


Edouard Hardy


On Mon, Sep 2, 2013 at 5:43 PM, arun  wrote:

> I guess in such situations,
>
>
> fun1<- function(mat){
>  if(nrow(mat)%%2==0){
>  j<- 2*seq_len(nrow(mat)/2)
>  b<- mat[j,]* mat[j-1,]
>  }
>  else {mat1<- mat[-nrow(mat),]
>  j<- 2*seq_len(nrow(mat1)/2)
>  b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
>   }
> b
> }
> fun1(A)
> # [,1] [,2] [,3]
> #[1,]4   10   18
> #[2,]   63   64   63
> #[3,]   18   104
>  fun1(Anew)
> # [,1] [,2] [,3]
> #[1,]4   10   18
> #[2,]   63   64   63
> #[3,]   18   104
> #[4,]135
>
>
> A.K.
>
>
>
> - Original Message -
> From: arun 
> To: Bert Gunter 
> Cc: R help 
> Sent: Monday, September 2, 2013 11:26 AM
> Subject: Re: [R] Product of certain rows in a matrix
>
> Hi Bert,
> Thanks.  It is a better solution.
>
> If nrow() is not even.
>
> Anew<- rbind(A,c(1,3,5))
> j<-seq_len(nrow(Anew)/2)###
>  Anew[j,]*Anew[j-1,]
> #Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays
>
>
> t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
>   [,1] [,2] [,3]
> 14   10   18
> 2   63   64   63
> 3   18   104
> 4135
>
> A.K.
>
>
>
>
>
>
> 
> From: Bert Gunter 
> To: arun 
> Cc: R help 
> Sent: Monday, September 2, 2013 10:55 AM
> Subject: Re: [R] Product of certain rows in a matrix
>
>
>
> These elaborate manipulations are unnecessary and inefficient. Use
> indexing instead:
>
> j <- 2*seq_len(nrow(A)/2)
> b <- A[j,]*A[j-1,]
> b
> [,1] [,2] [,3]
> [1,]4   10   18
> [2,]   63   64   63
> [3,]   18   104
>
> [,1] [,2] [,3]
> [1,]4   10   18
> [2,]   63   64   63
> [3,]   18   104
> [,1] [,2] [,3]
> [1,]4   10   18
> [2,]   63   64   63
> [3,]   18   104[,1] [,2] [,3]
> [1,]4   10   18
> [2,]   63   64   63
> [3,]   18   104
> [,1] [,2] [,3]
> [1,]4   10   18
> [2,]   63   64   63
> [3,]   18   104
>
>
>
>
>
> On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:
>
> Hi,
> >You could try:
> >
> >A<- matrix(unlist(read.table(text="
> >1 2 3
> >4 5 6
> >7 8 9
> >9 8 7
> >6 5 4
> >3 2 1
> >",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
> >
> >library(matrixStats)
>
> > res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
> > res1
> >#  [,1] [,2] [,3]
> >#14   10   18
> >#2   63   64   63
> >#3   18   104
> >
> >
>
> > res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
> > identical(res1,res2)
> >#[1] TRUE
> >
> >#or
> > t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x)
> apply(x,2,prod)))
> >
> >#or
> >library(plyr)
>
> > as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
> ># V1 V2 V3
> >#[1,]  4 10 18
> >#[2,] 63 64 63
> >#[3,] 18 10  4
> >
> >#or
> >do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
> colProds(A[x,])))
> >#or
> >A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
> > aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
> >#  X1 X2 X3
> >#1  4 10 18
> >#2 63 64 63
> >#3 18 10  4
> >
> >#or
> >library(data.table)
> >At<- data.table(A1,key='ID')
> >subset(At[,lapply(.SD,colProds),by=ID],select=-1)
> >#   X1 X2 X3
> >#1:  4 10 18
> >#2: 63 64 63
> >#3: 18 10  4
> >
> >A.K.
> >
> >
> >
> >
> >Hello,
> >
> >I have this matrix :
> >A =
> >1 2 3
> >4 5 6
> >7 8 9
> >9 8 7
> >6 5 4
> >3 2 1
> >
> >I would like to have this matrix (product of rows 2 by 2) :
> >A =
> >4 10 18
> >63 64 63
> >18 10 4
> >
> >Is it possible to do that without a loop ?
> >
> >Thank you in advance !
> >
> >__
> >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.
> >
>
>
> --
>
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
>
>
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
> __
> 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.


[R] Questions about 'bigmemory'

2013-09-02 Thread Zhenfei Yuan
Dear R users,

I'm now dealing with some big data set as big as 10GB, and my RAM got
16GB. Every time I run some models to this dataset using R, I have to
first load it into RAM via read.csv, which spends lots time.

I find the bigmemory package in the high performance task view of R, and
tried this package, found that it is really cool, especially for access
big matrix in shared memory through different R sessions.

My Question is that, when I call read.big.matrix on the .csv file (say
it aa.csv) the first time in R session 'R1', I would got one binary file
aa.bin and descriptor file aa.desc under specified backing path. I tried
to access the so called shared memory  via 'attach.big.matrix' in a
different R session 'R2', it works as I thought; however, when I quit
'R1', I could also access to the big matrix object via
attach.big.matrix('/path/to/aa.desc'), just like 'R1' still exists. I
copied the files 'aa.bin' and 'aa.desc' to another computer,
'attach.big.matrix' also worked on that one.


So, I don't know where the 'Shared Memory' is after turning 'R1' down;
is there differnece between existance and non-existance of 'R1'?

Best regards to you all,
Zhenfei

__
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] restructure my data

2013-09-02 Thread Wim Kreinen
My data is in this form: var has 3 conditions (0,1,2)

> df
   var  cauc
11 6462.3288
20 1585.2740
30 2481.6781
41  344.1781
50 8871.5753
62  816.7808
72 6031.3356
80 1013.5274
92 4913.5274
10   0 1517.2500

For the three conditions (0,1,2) I want the cauc-values to be listed like
this

0 1   2
1585,2740   6462,3288 816.7808
 2481.6781  344.1781   6031.3356
...

Thanks Wim

> dput (df)
structure(list(var = c(1, 0, 0, 1, 0, 2, 2, 0, 2, 0), cauc =
c(6462.32876712329,
1585.27397260274, 2481.67808219178, 344.178082191781, 8871.57534246575,
816.780821917808, 6031.33561643836, 1013.52739726027, 4913.52739726027,
1517.25)), .Names = c("var", "cauc"), row.names = c(NA, 10L), class =
"data.frame")
>

[[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] Sweave: printing an underscore in the output from an R command

2013-09-02 Thread Yihui Xie
I think Thierry meant gsub("_", "_", version$platform); he just
typed too quickly. The point is to escape _ using \, but then people
are often trapped in the dreams of dreams of dreams of backslashes
like the movie Inception. And then due to a long-standing bug in
Sweave for \Sexpr{} (sorry I forgot to report to R core), you will be
so confused that you can never wake up and come back to the reality.

Dream level 1: when you need a backslash in a character string, you
need "\\", which really means \; you think "\\_" should be good, but
no --

Dream level 2: when you need one literal \ in a regular expression as
the replacement expression, you need \\

Combine the two levels of dreams, you end up with "_".  in R
really means \\, which really means \ in regular expressions.

Now you are good at the regular expression level, but Sweave comes and
bites you, and that is due to this bug in the regular expression in
Sweave Noweb syntax:

> SweaveSyntaxNoweb$docexpr
[1] "Sexpr\\{([^\\}]*)\\}"

It should have been "Sexpr\\{([^}]*)\\}", i.e. } does not need to
be escaped inside [], and \\ will be interpreted literally inside [].
In your case, Sweave sees \ in \Sexpr{}, and the regular expression
stops matching there, and is unable to see } after \, so it believes
there is no inline R expressions in your document.

BTW, knitr does not have this bug and works well in your case:

\documentclass{article}
\begin{document}
\Sexpr{sub("_", "_", version$platform)}
\end{document}

Regards,
Yihui
--
Yihui Xie 
Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Mon, Sep 2, 2013 at 2:18 PM, David Epstein
 wrote:
> Dear Thierry,
>
> Your suggestion doesn't work on my version of R. Here's what I get
>> gsub("_", "\_", print(version$platform)
> Error: '\_' is an unrecognized escape in character string starting ""\_"
>> print(gsub("_", "\_", version$platform))
> Error: '\_' is an unrecognized escape in character string starting ""\_"
>
>> sub("_", "\\_", version$platform)
> [1] "x86_64-apple-darwin10.8.0"
> Sweave does not evaluate this expression when \Sexpr is applied and a tex 
> error results
>
>> sub("_", "\\\_", version$platform)
> Error: '\_' is an unrecognized escape in character string starting ""\\\_"
> Error message from R
>
>> sub("_", "_", version$platform)
> [1] "x86\\_64-apple-darwin10.8.0"
> R evaluates this. However, the above examples indicate a deficiency/possible 
> bug in the command sub, because sub does not seem to be able to output an 
> expression with a single backslash.
>
> I tried the previous version as follows in my .Rnw document
> \Sexpr{print(sub("_", "_", version$platform))}
> When Sweave is run, this expression is evaluated to illegal LaTeX
>
> David.
>
>
>
>
> On 2 Sep 2013, at 16:47, ONKELINX, Thierry wrote:
>
>> You have to escape the underscore
>>
>> \Sexpr{gsub("_", "\_", print(version$platform))}
>>
>> Best regards,
>>
>> Thierry
>>
>> 
>> Van: r-help-boun...@r-project.org [r-help-boun...@r-project.org] namens 
>> David Epstein [david.epst...@warwick.ac.uk]
>> Verzonden: maandag 2 september 2013 17:38
>> Aan: r-help@r-project.org
>> Onderwerp: [R] Sweave: printing an underscore in the output from an R command
>>
>> I am working with Sweave and would like to print out into my latex document 
>> the result of the R command
>> version$platform
>> So what I first tried in my .Rnw document was 
>> \Sexpr{print(version$platform)}.
>>
>> However, the output from this command is the string 
>> "x86_64-apple-darwin10.8.0" (without the quotes). This contains an 
>> underscore, which is a special character in tex and so I get an error 
>> message from latex.
>>
>> I can get round this by using sub to replace underscore with a space, but I 
>> would like to know how to print the underscore if I really wanted to do so.

__
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] Sweave: printing an underscore in the output from an R command

2013-09-02 Thread Duncan Murdoch

On 13-09-02 3:18 PM, David Epstein wrote:

Dear Thierry,

Your suggestion doesn't work on my version of R. Here's what I get

gsub("_", "\_", print(version$platform)

Error: '\_' is an unrecognized escape in character string starting ""\_"

print(gsub("_", "\_", version$platform))

Error: '\_' is an unrecognized escape in character string starting ""\_"


sub("_", "\\_", version$platform)

[1] "x86_64-apple-darwin10.8.0"
Sweave does not evaluate this expression when \Sexpr is applied and a tex error 
results


sub("_", "\\\_", version$platform)

Error: '\_' is an unrecognized escape in character string starting ""\\\_"
Error message from R


sub("_", "_", version$platform)

[1] "x86\\_64-apple-darwin10.8.0"
R evaluates this. However, the above examples indicate a deficiency/possible 
bug in the command sub, because sub does not seem to be able to output an 
expression with a single backslash.


The final result has a single backslash.  The print() function doubles 
it so you can tell it from an escape of the next letter.  If you use 
cat() instead of the implicit print() you'll see the text that is there.


I tried the previous version as follows in my .Rnw document
\Sexpr{print(sub("_", "_", version$platform))}
When Sweave is run, this expression is evaluated to illegal LaTeX



You need to give more details, e.g. what actually appeared in the .tex 
file and in what context, if you want help with this.


Duncan Murdoch

__
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] Product of certain rows in a matrix

2013-09-02 Thread arun
Hi,

Make sure you check the class of the columns.  I forgot about that.

str(mat1)
 int [1:10, 1:3] 1 2 10 18 2 16 1 18 12 19 ...


Convert it to numeric.
mat1New<- sapply(split(mat1,col(mat1)),as.numeric)

n<- 40
nrow(mat1New)%%40
#[1] 0


system.time({
j40<- n*seq_len(nrow(mat1New)/n)
 vec1<- rep("j40",n)
  res<- eval(parse(text= 
paste(paste0("mat1New","[",paste0(vec1,"-",seq(n)-1),",]"),collapse="*")
 ))
})
 #user  system elapsed 
 # 0.004   0.000   0.004 

system.time({
j <- seq_len(nrow(mat1New))%%n
b <- mat1New[j==0,]
for(i in seq_len(n-1))b <- b*mat1New[j==i,]
})
# user  system elapsed 
#  0.112   0.000   0.116 

all.equal(b,res)
#[1] TRUE


#if

nrow(mat1New)%%n!=0

For example:
n<- 22
nrow(mat1New)%%n
#[1] 10
system.time({
 mat2<-mat1New[seq(nrow(mat1New)-10),]
j22<- n*seq_len(nrow(mat2)/n)
vec1<- rep("j22",n)
res<- eval(parse(text= 
paste(paste0("mat2","[",paste0(vec1,"-",seq(n)-1),",]"),collapse="*")
)) 
resNew<-rbind(res,apply(tail(mat1New,10),2,prod)
)})

#  user  system elapsed 
 # 0.008   0.000   0.007 




A.K. 









- Original Message -
From: arun 
To: Edouard Hardy 
Cc: Bert Gunter 
Sent: Monday, September 2, 2013 3:31 PM
Subject: Re: [R] Product of certain rows in a matrix

There was a slight mistake in the end: 

I repeated vec1.  But, it doesn't matter as the warning messages are the same.


j40<- n*seq_len(nrow(mat1)/n)
 vec1<- rep("j40",n)
 res<- eval(parse(text= 
paste(paste0("mat1","[",paste0(vec1,"-",seq(n)-1),",]"),collapse="*")
 ))

Warning messages:
1: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :
  NAs produced by integer overflow
2: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :
  NAs produced by integer overflow
3: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :
  NAs produced by integer overflow
4: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :
  NAs produced by integer overflow
5: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :
  NAs produced by integer overflow
6: In mat1[j40 - 0, ] * mat1[j40 - 1, ] * mat1[j40 - 2, ] * mat1[j40 -  :




- Original Message -
From: arun 
To: Edouard Hardy 
Cc: R help ; Bert Gunter 
Sent: Monday, September 2, 2013 3:25 PM
Subject: Re: [R] Product of certain rows in a matrix

Hi,
You can try this:
n<- 3
j3<-n*seq_len(nrow(A)/n)
vec1<- rep("j3",n)
eval(parse(text=paste0("A","[",paste0(vec1,"-",seq(n)-1),",]",collapse="*")))
# [,1] [,2] [,3]
#[1,]   28   80  162
#[2,]  162   80   28

Just saw Bert's new solution:
n<-3
 j <- seq_len(nrow(A))%%n
 b <- A[j==0,]
 for(i in seq_len(n-1))b <- b*A[j==i,]
 b
# [,1] [,2] [,3]
#[1,]   28   80  162
#[2,]  162   80   28







#For the bigger dataset with large "n, these methods may not work:


set.seed(28)
mat1<- matrix(sample(1:20,1e5*3,replace=TRUE),ncol=3)

n<- 40
nrow(mat1)%%40
#[1] 0


j <- seq_len(nrow(mat1))%%n
b <- mat1[j==0,]
for(i in seq_len(n-1))b <- b*mat1[j==i,]

Warning messages:
1: In b * mat1[j == i, ] : NAs produced by integer overflow
2: In b * mat1[j == i, ] : NAs produced by integer overflow
3: In b * mat1[j == i, ] : NAs produced by integer overflow
4: In b * mat1[j == i, ] : NAs produced by integer overflow
5: In b * mat1[j == i, ] : NAs produced by integer overflow
6: In b * mat1[j == i, ] : NAs produced by integer overflow
7: In b * mat1[j == i, ] : NAs produced by integer overflow
8: In b * mat1[j == i, ] : NAs produced by integer overflow
9: In b * mat1[j == i, ] : NAs produced by integer overflow
10: In b * mat1[j == i, ] : NAs produced by integer overflow

head(b,3)
# [,1] [,2] [,3]
#[1,]   NA   NA   NA
#[2,]   NA   NA   NA
#[3,]   NA   NA   NA


j40<- n*seq_len(nrow(mat1)/n)
 vec1<- rep("j40",n)
 vec1<- rep("j22",n)
 res<- eval(parse(text= 
paste(paste0("mat1","[",paste0(vec1,"-",seq(n)-1),",]"),collapse="*")
 ))
Warning messages:
1: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
  NAs produced by integer overflow
2: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
  NAs produced by integer overflow
3: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
  NAs produced by integer overflow
4: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
 head(res,3)
# [,1] [,2] [,3]
#[1,]   NA   NA   NA
#[2,]   NA   NA   NA
#[3,]   NA   NA   NA


A.K.






From: Edouard Hardy 
To: arun  
Sent: Monday, September 2, 2013 2:32 PM
Subject: Re: [R] Product of certain rows in a matrix



Yes, n is 250 or more...



Edouard Hardy



On Mon, Sep 2, 2013 at 8:31 PM, arun  wrote:

Also, BTW, are you looking for n>100?
>
>
>
>
>
>





On Mon, Sep 2, 2013 at 8:27 PM, arun  wrote:


>
>Hi,
>
>Not sure I understand your question.  If you don't know "n", then how are you 
>applying other solutions also..
>A.K.
>
>
>
>Again, thank you for your help.
>
>I understand Bert's solution but this is possible only if I know n.
>
>How can 

Re: [R] Fisher test for a more than two group of genes‏

2013-09-02 Thread David Winsemius

On Aug 28, 2013, at 11:53 AM, Gabriel Wajnberg wrote:
> 
> Good Afternoon,
> My name is Gabriel, I'm doing an analysis if there is increase or decrease in 
> dependence on the mutated genes, using 3 or more genes using the fisher exact 
> test.I performed with success an analysis for two genes using fisher.test( ). 
> example of the 2x2 contigency table:

Edited original table:

>   Gene A mutated | Gene A normal
> Gene B mutated| 26   |12
> 
> Gene B normal |  10  |50
> 
> Now I'm wondering how can I perform the analysis for 3 genes (and construct 
> the contigency table), as follows: Gene A mutated, Gene A normal, Gene B 
> mutated, Gene B normal, Gene C mutated and Gene C normal. How do I perform a 
> fisher test using fisher.test( ) function using this data (3x3 contigency 
> table)?Can someone help me ?
>   
>   
>   [[alternative HTML version deleted]]

I'm guessing you failed to read the Posting Guide where you are asked not to 
post in HTML format. I also don't understand what problems youa re having when 
you use fisr.test with a 3x3 table. Its help page says it acna handle a 2 
dimnensional contingency table.

-- 

David Winsemius
Alameda, CA, USA

__
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] Product of certain rows in a matrix

2013-09-02 Thread arun
Hi,
You can try this:
n<- 3
j3<-n*seq_len(nrow(A)/n)
vec1<- rep("j3",n)
eval(parse(text=paste0("A","[",paste0(vec1,"-",seq(n)-1),",]",collapse="*")))
# [,1] [,2] [,3]
#[1,]   28   80  162
#[2,]  162   80   28

Just saw Bert's new solution:
n<-3
 j <- seq_len(nrow(A))%%n
 b <- A[j==0,]
 for(i in seq_len(n-1))b <- b*A[j==i,]
 b
# [,1] [,2] [,3]
#[1,]   28   80  162
#[2,]  162   80   28







#For the bigger dataset with large "n, these methods may not work:


set.seed(28)
mat1<- matrix(sample(1:20,1e5*3,replace=TRUE),ncol=3)

n<- 40
nrow(mat1)%%40
#[1] 0


j <- seq_len(nrow(mat1))%%n
b <- mat1[j==0,]
for(i in seq_len(n-1))b <- b*mat1[j==i,]

Warning messages:
1: In b * mat1[j == i, ] : NAs produced by integer overflow
2: In b * mat1[j == i, ] : NAs produced by integer overflow
3: In b * mat1[j == i, ] : NAs produced by integer overflow
4: In b * mat1[j == i, ] : NAs produced by integer overflow
5: In b * mat1[j == i, ] : NAs produced by integer overflow
6: In b * mat1[j == i, ] : NAs produced by integer overflow
7: In b * mat1[j == i, ] : NAs produced by integer overflow
8: In b * mat1[j == i, ] : NAs produced by integer overflow
9: In b * mat1[j == i, ] : NAs produced by integer overflow
10: In b * mat1[j == i, ] : NAs produced by integer overflow

head(b,3)
# [,1] [,2] [,3]
#[1,]   NA   NA   NA
#[2,]   NA   NA   NA
#[3,]   NA   NA   NA


j40<- n*seq_len(nrow(mat1)/n)
 vec1<- rep("j40",n)
 vec1<- rep("j22",n)
 res<- eval(parse(text= 
paste(paste0("mat1","[",paste0(vec1,"-",seq(n)-1),",]"),collapse="*")
 ))
Warning messages:
1: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
  NAs produced by integer overflow
2: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
  NAs produced by integer overflow
3: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
  NAs produced by integer overflow
4: In mat1[j22 - 0, ] * mat1[j22 - 1, ] * mat1[j22 - 2, ] * mat1[j22 -  :
 head(res,3)
# [,1] [,2] [,3]
#[1,]   NA   NA   NA
#[2,]   NA   NA   NA
#[3,]   NA   NA   NA


A.K.






From: Edouard Hardy 
To: arun  
Sent: Monday, September 2, 2013 2:32 PM
Subject: Re: [R] Product of certain rows in a matrix



Yes, n is 250 or more...



Edouard Hardy



On Mon, Sep 2, 2013 at 8:31 PM, arun  wrote:

Also, BTW, are you looking for n>100?
>
>
>
>
>
>





On Mon, Sep 2, 2013 at 8:27 PM, arun  wrote:


>
>Hi,
>
>Not sure I understand your question.  If you don't know "n", then how are you 
>applying other solutions also..
>A.K.
>
>
>
>Again, thank you for your help.
>
>I understand Bert's solution but this is possible only if I know n.
>
>How can I do A[j3,]*A[j3-1,]*A[j3-2,] (n=3) for n terms ?
>
>
>
>- Original Message -
>From: arun 
>To: Edouard Hardy 
>Cc: R help ; Bert Gunter 
>
>Sent: Monday, September 2, 2013 1:57 PM
>Subject: Re: [R] Product of certain rows in a matrix
>
>HI,
>You could modify Bert's solution:
>n<-3
>
>j3<-n*seq_len(nrow(A)/n)
>A[j3,]*A[j3-1,]*A[j3-2,]  ##assuming that nrow(dataset)%%n==0
># [,1] [,2] [,3]
>#[1,]   28   80  162
>#[2,]  162   80   28
>
>
>#Speed comparison
>
>
>set.seed(28)
>mat1<- matrix(sample(1:20,1e5*3,replace=TRUE),ncol=3)
>
>n<-4
>system.time({res1<- 
>t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
> apply(x,2,prod))) })
>#  user  system elapsed
>#  8.508   0.620   9.146
>system.time({res2<- 
>t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
> Reduce("*",as.data.frame(t(x) })
># user  system elapsed
>#  8.556   0.000   8.566
>
>A1<- data.frame(mat1,ID=as.numeric(gl(nrow(mat1),n,nrow(mat1
> system.time({res3<- aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]})
># user  system elapsed
># 11.536   0.000  11.553
>
>
>nrow(mat1)%%n
>#[1] 0
>system.time({j4<- n*seq_len(nrow(mat1)/n)
>        res5<- mat1[j4,]*mat1[j4-1,]*mat1[j4-2,]*mat1[j4-3,]
>      })
>
># user  system elapsed
>#  0.004   0.000   0.004
>
> dimnames(res2)<- dimnames(res5)
>identical(res2,res5)
>#[1] TRUE
>
>
>#if
>n<-6
> nrow(mat1)%%6
>#[1] 4
>
>
>system.time({
> mat2<-mat1[seq(nrow(mat1)-4),]
>j6<- n*seq_len(nrow(mat2)/n)
> res6<- mat2[j6,]*mat2[j6-1,]*mat2[j6-2,]*mat2[j6-3,]*mat2[j6-4,]*mat2[j6-5,]
>res6New<-rbind(res6,apply(tail(mat1,4),2,prod)
>)})
>
>#  user  system elapsed
> # 0.004   0.000   0.006
>
>
>
>system.time({res6Alt<-
>t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
> Reduce("*",as.data.frame(t(x) })
>#user  system elapsed
> # 5.576   0.000   5.583
>dimnames(res6Alt)<- dimnames(res6New)
>
>
>all.equal(res6New,res6Alt)
>#[1] TRUE
>
>
>A.K.
>
>
>
>As you said, this is very lng.
>Do you have a better solution on big data ?
>
>
>
>- Original Message -
>From: arun 
>To: Edouard Hardy 
>Cc: R help ; Bert Gunter 
>Sent: Monday, September 2, 2013 12:07 PM
>Subject: Re: [R] Product of certain rows in a matrix
>
>
>
>Hi,
>No problem.
>n<- 4
>
>t(sapply(split(as.

Re: [R] Sweave: printing an underscore in the output from an R command

2013-09-02 Thread David Epstein
Dear Thierry,

Your suggestion doesn't work on my version of R. Here's what I get
> gsub("_", "\_", print(version$platform)
Error: '\_' is an unrecognized escape in character string starting ""\_"
> print(gsub("_", "\_", version$platform))
Error: '\_' is an unrecognized escape in character string starting ""\_"

> sub("_", "\\_", version$platform)
[1] "x86_64-apple-darwin10.8.0"
Sweave does not evaluate this expression when \Sexpr is applied and a tex error 
results

> sub("_", "\\\_", version$platform)
Error: '\_' is an unrecognized escape in character string starting ""\\\_"
Error message from R

> sub("_", "_", version$platform)
[1] "x86\\_64-apple-darwin10.8.0"
R evaluates this. However, the above examples indicate a deficiency/possible 
bug in the command sub, because sub does not seem to be able to output an 
expression with a single backslash.

I tried the previous version as follows in my .Rnw document
\Sexpr{print(sub("_", "_", version$platform))}
When Sweave is run, this expression is evaluated to illegal LaTeX

David.




On 2 Sep 2013, at 16:47, ONKELINX, Thierry wrote:

> You have to escape the underscore
> 
> \Sexpr{gsub("_", "\_", print(version$platform))}
> 
> Best regards,
> 
> Thierry
> 
> 
> Van: r-help-boun...@r-project.org [r-help-boun...@r-project.org] namens David 
> Epstein [david.epst...@warwick.ac.uk]
> Verzonden: maandag 2 september 2013 17:38
> Aan: r-help@r-project.org
> Onderwerp: [R] Sweave: printing an underscore in the output from an R command
> 
> I am working with Sweave and would like to print out into my latex document 
> the result of the R command
> version$platform
> So what I first tried in my .Rnw document was \Sexpr{print(version$platform)}.
> 
> However, the output from this command is the string 
> "x86_64-apple-darwin10.8.0" (without the quotes). This contains an 
> underscore, which is a special character in tex and so I get an error message 
> from latex.
> 
> I can get round this by using sub to replace underscore with a space, but I 
> would like to know how to print the underscore if I really wanted to do so.
> 
> __
> 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.
> * * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
> Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer 
> en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd 
> is door een geldig ondertekend document.
> The views expressed in this message and any annex are purely those of the 
> writer and may not be regarded as stating an official position of INBO, as 
> long as the message is not confirmed by a duly signed document.
> 

__
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] Product of certain rows in a matrix

2013-09-02 Thread Bert Gunter
## For arbitrary n, just loop over the lag!

Here's an alternative version using logical instead of numerical indexing
(assumes nrow(A) %% n = 0).
You could also use the numerical indexing as before, of course. Doubt that
it would make much of a speed difference, but you can try it and see.

j <- seq_len(nrow(A))%%n
b <- A[j==0,]
for(i in seq_len(n-1))b <- b*A[j==i,]
b



-- Bert


On Mon, Sep 2, 2013 at 10:57 AM, arun  wrote:

> HI,
> You could modify Bert's solution:
> n<-3
>
> j3<-n*seq_len(nrow(A)/n)
> A[j3,]*A[j3-1,]*A[j3-2,]  ##assuming that nrow(dataset)%%n==0
> # [,1] [,2] [,3]
> #[1,]   28   80  162
> #[2,]  162   80   28
>
>
> #Speed comparison
>
>
> set.seed(28)
> mat1<- matrix(sample(1:20,1e5*3,replace=TRUE),ncol=3)
>
> n<-4
> system.time({res1<-
> t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
> apply(x,2,prod))) })
> #  user  system elapsed
> #  8.508   0.620   9.146
> system.time({res2<-
> t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
> Reduce("*",as.data.frame(t(x) })
> # user  system elapsed
> #  8.556   0.000   8.566
>
> A1<- data.frame(mat1,ID=as.numeric(gl(nrow(mat1),n,nrow(mat1
>  system.time({res3<- aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]})
> # user  system elapsed
> # 11.536   0.000  11.553
>
>
> nrow(mat1)%%n
> #[1] 0
> system.time({j4<- n*seq_len(nrow(mat1)/n)
> res5<- mat1[j4,]*mat1[j4-1,]*mat1[j4-2,]*mat1[j4-3,]
>   })
>
> # user  system elapsed
> #  0.004   0.000   0.004
>
>  dimnames(res2)<- dimnames(res5)
> identical(res2,res5)
> #[1] TRUE
>
>
> #if
> n<-6
>  nrow(mat1)%%6
> #[1] 4
>
>
> system.time({
>  mat2<-mat1[seq(nrow(mat1)-4),]
> j6<- n*seq_len(nrow(mat2)/n)
>  res6<-
> mat2[j6,]*mat2[j6-1,]*mat2[j6-2,]*mat2[j6-3,]*mat2[j6-4,]*mat2[j6-5,]
> res6New<-rbind(res6,apply(tail(mat1,4),2,prod)
> )})
>
> #  user  system elapsed
>  # 0.004   0.000   0.006
>
>
>
> system.time({res6Alt<-
> t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
> Reduce("*",as.data.frame(t(x) })
> #user  system elapsed
>  # 5.576   0.000   5.583
> dimnames(res6Alt)<- dimnames(res6New)
>
>
> all.equal(res6New,res6Alt)
> #[1] TRUE
>
>
> A.K.
>
>
>
> As you said, this is very lng.
> Do you have a better solution on big data ?
>
>
>
> - Original Message -
> From: arun 
> To: Edouard Hardy 
> Cc: R help ; Bert Gunter 
> Sent: Monday, September 2, 2013 12:07 PM
> Subject: Re: [R] Product of certain rows in a matrix
>
>
>
> Hi,
> No problem.
> n<- 4
>
> t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,function(x)
> apply(x,2,prod)))
>
> #  V1  V2   V3
> #1 252 640 1134
> #2  18  30   20
>
>
> This could be a bit slow if you have big dataset.
>
>
> A.K.
>
>
>
> 
> From: Edouard Hardy 
> To: arun 
> Cc: R help 
> Sent: Monday, September 2, 2013 11:58 AM
> Subject: Re: [R] Product of certain rows in a matrix
>
>
>
> Thank you A.K.
> And do you have a solution without installing any package ?
> Thank you in advance.
> E.H.
>
>
>
> Edouard Hardy
>
>
>
> On Mon, Sep 2, 2013 at 5:56 PM, arun  wrote:
>
>
> >
> >HI,
> >In my first solutions:
> > n<-3
>
> > t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
> >#  [,1] [,2] [,3]
> >#1   28   80  162
> >#2  162   80   28
> >#3135
> > n<-4
>
> > t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
> >#  [,1] [,2] [,3]
> >#1  252  640 1134
> >#2   18   30   20
> >
> >A.K.
> >
> >
> >From: Edouard Hardy 
> >To: arun 
> >Cc: Bert Gunter ; R help 
> >Sent: Monday, September 2, 2013 11:46 AM
> >
> >Subject: Re: [R] Product of certain rows in a matrix
> >
> >
> >
> >Thank you all for your responses.
> >The real problem is that all your answer work for products 2 by 2.
> >I now have to do the product n by n row.
> >Do you have a solution ?
> >Thank you in advance,
> >E.H.
> >
> >
> >
> >Edouard Hardy
> >
> >
> >
> >On Mon, Sep 2, 2013 at 5:43 PM, arun  wrote:
> >
> >I guess in such situations,
> >>
> >>
> >>fun1<- function(mat){
> >> if(nrow(mat)%%2==0){
> >> j<- 2*seq_len(nrow(mat)/2)
> >> b<- mat[j,]* mat[j-1,]
> >> }
> >> else {mat1<- mat[-nrow(mat),]
> >> j<- 2*seq_len(nrow(mat1)/2)
> >> b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
> >>  }
> >>b
> >>}
> >>fun1(A)
> >># [,1] [,2] [,3]
> >>
> >>#[1,]4   10   18
> >>#[2,]   63   64   63
> >>#[3,]   18   104
> >> fun1(Anew)
> >># [,1] [,2] [,3]
> >>
> >>#[1,]4   10   18
> >>#[2,]   63   64   63
> >>#[3,]   18   104
> >>#[4,]135
> >>
> >>
> >>A.K.
> >>
> >>
> >>
> >>
> >>- Original Message -
> >>From: arun 
> >>To: Bert Gunter 
> >>Cc: R help 
> >>
> >>Sent: Monday, September 2, 2013 11:26 AM
> >>Subject: Re: [R] Product of certain rows in a matrix
> >>
> >>Hi Bert,
> >>Thanks.  It is a better solution.
> >>
> >>If nrow() is not even.
> >>
> >>Anew<- r

Re: [R] How to catch errors regarding the hessian in 'optim'

2013-09-02 Thread Simon Zehnder
Dear John,

thank you very much for your answer. I take a look at these packages (Rvmmin 
and Rcgmin). That sounds very interesting. 

For the example: The method relies on data which I always try to avoid to send 
on the r-help list - not that my data is confidential - but it becomes even 
more cumbersome when sending datasets over the list. I made the experience, 
that in such case answers are rare. Maybe you have a suggestion if and how 
users should send data with their code. I am interested in your opinion and 
thankful for sharing it.

Best

Simon


On Sep 2, 2013, at 4:42 PM, Prof J C Nash (U30A)  wrote:

> This may be one of the many mysteries of the internals of L-BFGS-B, which I 
> have found fails from time to time. That is one of the reasons for Rvmmin and 
> Rcgmin (and hopefully sooner rather than later Rtn - a truncated Newton 
> method, currently working for unconstrained problems, but still glitchy for 
> bounds constraints). These are all-R codes so that users and developers can 
> get inside to control special situations.
> 
> If you have a test problem -- the infamous reproducible example -- there are 
> several of us who can likely help to sort out your troubles.
> 
> JN
> 
> 
> On 13-09-02 06:00 AM, r-help-requ...@r-project.org wrote:
>> Message: 10
>> Date: Sun, 1 Sep 2013 17:09:35 +0200
>> From: Simon Zehnder
>> To: R-help help
>> Subject: [R] How to catch errors regarding the hessian in 'optim'
>> Message-ID:
>> Content-Type: text/plain; charset=us-ascii
>> 
>> Dear R-Users and R-Developers,
>> 
>> in a comparison between two different estimation approaches I would like to 
>> catch errors from optim regarding the hessian matrix.
>> 
>> I use optim with method = "L-BFGS-B" thereby relying on numerical 
>> differentiation for the hessian matrix. I do know, that the estimation 
>> approach that uses numerical optimization has sometimes problems with 
>> singular hessian matrices and I consider it as one of its disadvantages of 
>> this method. To show the frequency of such problems in my simulation study I 
>> have to set 'hessian = TRUE' and to collect the errors from optim regarding 
>> the hessian.
>> 
>> Now I am a little stucked how I could catch specifically errors from the 
>> hessian matrix in 'optim'. I do know that such errors are thrown most 
>> certainly from function 'La_solve' in Lapack.c. Does anyone has an idea how 
>> I could solve this task (clearly with tryCatch but how to choose only errors 
>> for the hessian)?
>> 
>> 
>> Best
>> 
>> Simon

__
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] Product of certain rows in a matrix

2013-09-02 Thread arun
HI,
You could modify Bert's solution:
n<-3

j3<-n*seq_len(nrow(A)/n)
A[j3,]*A[j3-1,]*A[j3-2,]  ##assuming that nrow(dataset)%%n==0
# [,1] [,2] [,3]
#[1,]   28   80  162
#[2,]  162   80   28


#Speed comparison


set.seed(28)
mat1<- matrix(sample(1:20,1e5*3,replace=TRUE),ncol=3)

n<-4
system.time({res1<- 
t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
 apply(x,2,prod))) })
#  user  system elapsed 
#  8.508   0.620   9.146 
system.time({res2<- 
t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
 Reduce("*",as.data.frame(t(x) })
# user  system elapsed 
#  8.556   0.000   8.566 

A1<- data.frame(mat1,ID=as.numeric(gl(nrow(mat1),n,nrow(mat1
 system.time({res3<- aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]})
# user  system elapsed 
# 11.536   0.000  11.553 


nrow(mat1)%%n
#[1] 0
system.time({j4<- n*seq_len(nrow(mat1)/n)
        res5<- mat1[j4,]*mat1[j4-1,]*mat1[j4-2,]*mat1[j4-3,]
      })

# user  system elapsed 
#  0.004   0.000   0.004 

 dimnames(res2)<- dimnames(res5)
identical(res2,res5)
#[1] TRUE


#if
n<-6
 nrow(mat1)%%6
#[1] 4


system.time({
 mat2<-mat1[seq(nrow(mat1)-4),]
j6<- n*seq_len(nrow(mat2)/n)
 res6<- mat2[j6,]*mat2[j6-1,]*mat2[j6-2,]*mat2[j6-3,]*mat2[j6-4,]*mat2[j6-5,]
res6New<-rbind(res6,apply(tail(mat1,4),2,prod)
)})

#  user  system elapsed 
 # 0.004   0.000   0.006 



system.time({res6Alt<- 
t(sapply(split(as.data.frame(mat1),as.numeric(gl(nrow(mat1),n,nrow(mat1,function(x)
 Reduce("*",as.data.frame(t(x) })
#user  system elapsed 
 # 5.576   0.000   5.583 
dimnames(res6Alt)<- dimnames(res6New)


all.equal(res6New,res6Alt)
#[1] TRUE


A.K.



As you said, this is very lng. 
Do you have a better solution on big data ? 



- Original Message -
From: arun 
To: Edouard Hardy 
Cc: R help ; Bert Gunter 
Sent: Monday, September 2, 2013 12:07 PM
Subject: Re: [R] Product of certain rows in a matrix



Hi,
No problem.
n<- 4

t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,function(x)
 apply(x,2,prod)))  

#  V1  V2   V3
#1 252 640 1134
#2  18  30   20


This could be a bit slow if you have big dataset.


A.K.




From: Edouard Hardy 
To: arun  
Cc: R help  
Sent: Monday, September 2, 2013 11:58 AM
Subject: Re: [R] Product of certain rows in a matrix



Thank you A.K.
And do you have a solution without installing any package ?
Thank you in advance.
E.H.



Edouard Hardy



On Mon, Sep 2, 2013 at 5:56 PM, arun  wrote:


>
>HI,
>In my first solutions:
> n<-3
> 
>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
>#  [,1] [,2] [,3]
>#1   28   80  162
>#2  162   80   28
>#3    1    3    5
> n<-4
> 
>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
>#  [,1] [,2] [,3]
>#1  252  640 1134
>#2   18   30   20
>
>A.K.
>
>
>From: Edouard Hardy 
>To: arun 
>Cc: Bert Gunter ; R help 
>Sent: Monday, September 2, 2013 11:46 AM
>
>Subject: Re: [R] Product of certain rows in a matrix
>
>
>
>Thank you all for your responses.
>The real problem is that all your answer work for products 2 by 2.
>I now have to do the product n by n row.
>Do you have a solution ?
>Thank you in advance,
>E.H. 
>
>
>
>Edouard Hardy
>
>
>
>On Mon, Sep 2, 2013 at 5:43 PM, arun  wrote:
>
>I guess in such situations,
>>
>>
>>fun1<- function(mat){
>> if(nrow(mat)%%2==0){
>> j<- 2*seq_len(nrow(mat)/2)
>> b<- mat[j,]* mat[j-1,]
>> }
>> else {mat1<- mat[-nrow(mat),]
>> j<- 2*seq_len(nrow(mat1)/2)
>> b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
>>  }
>>b
>>}
>>fun1(A)
>># [,1] [,2] [,3]
>>
>>#[1,]    4   10   18
>>#[2,]   63   64   63
>>#[3,]   18   10    4
>> fun1(Anew)
>># [,1] [,2] [,3]
>>
>>#[1,]    4   10   18
>>#[2,]   63   64   63
>>#[3,]   18   10    4
>>#[4,]    1    3    5
>>
>>
>>A.K.
>>
>>
>>
>>
>>- Original Message -
>>From: arun 
>>To: Bert Gunter 
>>Cc: R help 
>>
>>Sent: Monday, September 2, 2013 11:26 AM
>>Subject: Re: [R] Product of certain rows in a matrix
>>
>>Hi Bert,
>>Thanks.  It is a better solution.
>>
>>If nrow() is not even.
>>
>>Anew<- rbind(A,c(1,3,5))
>>j<-seq_len(nrow(Anew)/2)###
>> Anew[j,]*Anew[j-1,]
>>#Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays
>>
>>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
>>  [,1] [,2] [,3]
>>1    4   10   18
>>2   63   64   63
>>3   18   10    4
>>4    1    3    5
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>
>>From: Bert Gunter 
>>To: arun 
>>Cc: R help 
>>Sent: Monday, September 2, 2013 10:55 AM
>>Subject: Re: [R] Product of certain rows in a matrix
>>
>>
>>
>>These elaborate manipulations are unnecessary and inefficient. Use indexing 
>>instead:
>>
>>j <- 2*seq_len(nrow(A)/2)
>>b <- A[j,]*A[j-1,]
>>b
>>[,1] [,2] [,3]
>>[1,]    4   10   18
>>[2,]   63   64   63
>>[3,]   18   10    4
>>
>>[,1] [,2] [,3]
>>[1,]4   10   18
>>[2,]   63   6

[R] quoted expressions in microbenchmark

2013-09-02 Thread Prof J C Nash (U30A)
I use microbenchmark to time various of my code segments and find it 
very useful. However, by accident I called it with the expression I 
wanted to time quoted. This simply measured the time to evaluate the 
quote. The following illustrates the difference. When explained, the 
issue is obvious, but I spun some wheels for a while and the example may 
help others.


> rm(list=ls()) # clear workspace
> genrose.g <- function(x, gs=100){
> # vectorized gradient for genrose.f
> n <- length(x)
> gg <- as.vector(rep(0, n))
> tn <- 2:n
> tn1 <- tn - 1
> z1 <- x[tn] - x[tn1]^2
> z2 <- 1 - x[tn]
> gg[tn] <- 2 * (gs * z1 - z2)
> gg[tn1] <- gg[tn1] - 4 * gs * x[tn1] * z1
> return(gg)
> }
> require(microbenchmark)
> trep=1000
> xx<-rep(pi/2,20)
> atq<-microbenchmark("genrose.g(xx)", times=trep)
> print(atq)
> at<-microbenchmark(genrose.g(xx), times=trep)
> print(at)

which gives

> source("tmbench.R")
Unit: nanoseconds
expr min  lq median  uq   max neval
 "genrose.g(xx)"  70 420489 489 12851  1000
Unit: microseconds
  exprmin lq median uq  max neval
 genrose.g(xx) 47.982 49.868 50.426 51.404 3523.566  1000

Thanks to Olaf Mersmann for a quick response to set me straight. He 
pointed out that sometimes one wants to measure the time to evaluate a 
character string, as in the following.


  > microbenchmark(NULL, "", "asdf", "12345678901234567890", times=1000L)
  Unit: nanoseconds
 expr min lq median uq max neval
 NULL  24 25 28 29 161  1000
   ""  24 25 28 29 121  1000
   "asdf"  24 25 28 29 161  1000
   "12345678901234567890"  24 28 28 29 542  1000


John Nash

__
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] Meaning of "Integer,19"

2013-09-02 Thread arun
Hi,

If you check ?str()
str(zseq)
#List of 6
# $ : int [1:19] 1 2 3 4 5 6 7 8 9 10 ...
# $ : int [1:20] 1 2 3 4 5 6 7 8 9 10 ...
# $ : int [1:21] 1 2 3 4 5 6 7 8 9 10 ...
# $ : int [1:22] 1 2 3 4 5 6 7 8 9 10 ...
# $ : int [1:23] 1 2 3 4 5 6 7 8 9 10 ...
# $ : int [1:24] 1 2 3 4 5 6 7 8 9 10 ...
# - attr(*, "dim")= int [1:2] 2 3


Can access each list element by:
zseq[[1]]
# [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19


#Integer is the class and 19, 20, 21,... are the total number of elements

  lapply(zseq,unlist)
[[1]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19

[[2]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

[[3]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21

[[4]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22

[[5]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

[[6]]
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24


A.K.

- Original Message -
From: David Epstein 
To: r-help@r-project.org
Cc: 
Sent: Monday, September 2, 2013 11:42 AM
Subject: [R] Meaning of "Integer,19"

I tried example('apply'). Among the various examples, there was the following:

apply> z <- array(1:24, dim = 2:4)
apply> zseq <- apply(z, 1:2, function(x) seq_len(max(x)))
apply> zseq         ## a 2 x 3 matrix
     [,1]       [,2]       [,3]
[1,] Integer,19 Integer,21 Integer,23
[2,] Integer,20 Integer,22 Integer,24

The entry "Integer,19" seems to mean the list of integers [1:19], though I'm 
just guessing. Possibly it means some list of 19 integers.

Questions:
Is the notation "Integer,19" documented somewhere? I can't find it.
How might one proceed to find out the meaning of this notation if one didn't 
know it before?
The actual substance of my question is a request for advice on how, in general, 
to look for documentation in R.

I know about help.start(), help.search() and RSiteSearch(). Also, I know how to 
search the archives of r-help. Are there other methods of searching R that I 
should try?
Usually I get more hits than I can cope with.
Thanks










    [[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.


__
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] Product of certain rows in a matrix

2013-09-02 Thread arun


Hi,
No problem.
n<- 4

t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,function(x)
 apply(x,2,prod)))  

#  V1  V2   V3
#1 252 640 1134
#2  18  30   20


This could be a bit slow if you have big dataset.


A.K.




From: Edouard Hardy 
To: arun  
Cc: R help  
Sent: Monday, September 2, 2013 11:58 AM
Subject: Re: [R] Product of certain rows in a matrix



Thank you A.K.
And do you have a solution without installing any package ?
Thank you in advance.
E.H.



Edouard Hardy



On Mon, Sep 2, 2013 at 5:56 PM, arun  wrote:


>
>HI,
>In my first solutions:
> n<-3
> 
>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
>#  [,1] [,2] [,3]
>#1   28   80  162
>#2  162   80   28
>#3    1    3    5
> n<-4
> 
>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
>#  [,1] [,2] [,3]
>#1  252  640 1134
>#2   18   30   20
>
>A.K.
>
>
>From: Edouard Hardy 
>To: arun 
>Cc: Bert Gunter ; R help 
>Sent: Monday, September 2, 2013 11:46 AM
>
>Subject: Re: [R] Product of certain rows in a matrix
>
>
>
>Thank you all for your responses.
>The real problem is that all your answer work for products 2 by 2.
>I now have to do the product n by n row.
>Do you have a solution ?
>Thank you in advance,
>E.H. 
>
>
>
>Edouard Hardy
>
>
>
>On Mon, Sep 2, 2013 at 5:43 PM, arun  wrote:
>
>I guess in such situations,
>>
>>
>>fun1<- function(mat){
>> if(nrow(mat)%%2==0){
>> j<- 2*seq_len(nrow(mat)/2)
>> b<- mat[j,]* mat[j-1,]
>> }
>> else {mat1<- mat[-nrow(mat),]
>> j<- 2*seq_len(nrow(mat1)/2)
>> b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
>>  }
>>b
>>}
>>fun1(A)
>># [,1] [,2] [,3]
>>
>>#[1,]    4   10   18
>>#[2,]   63   64   63
>>#[3,]   18   10    4
>> fun1(Anew)
>># [,1] [,2] [,3]
>>
>>#[1,]    4   10   18
>>#[2,]   63   64   63
>>#[3,]   18   10    4
>>#[4,]    1    3    5
>>
>>
>>A.K.
>>
>>
>>
>>
>>- Original Message -
>>From: arun 
>>To: Bert Gunter 
>>Cc: R help 
>>
>>Sent: Monday, September 2, 2013 11:26 AM
>>Subject: Re: [R] Product of certain rows in a matrix
>>
>>Hi Bert,
>>Thanks.  It is a better solution.
>>
>>If nrow() is not even.
>>
>>Anew<- rbind(A,c(1,3,5))
>>j<-seq_len(nrow(Anew)/2)###
>> Anew[j,]*Anew[j-1,]
>>#Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays
>>
>>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
>>  [,1] [,2] [,3]
>>1    4   10   18
>>2   63   64   63
>>3   18   10    4
>>4    1    3    5
>>
>>A.K.
>>
>>
>>
>>
>>
>>
>>
>>From: Bert Gunter 
>>To: arun 
>>Cc: R help 
>>Sent: Monday, September 2, 2013 10:55 AM
>>Subject: Re: [R] Product of certain rows in a matrix
>>
>>
>>
>>These elaborate manipulations are unnecessary and inefficient. Use indexing 
>>instead:
>>
>>j <- 2*seq_len(nrow(A)/2)
>>b <- A[j,]*A[j-1,]
>>b
>>[,1] [,2] [,3]
>>[1,]    4   10   18
>>[2,]   63   64   63
>>[3,]   18   10    4
>>
>>[,1] [,2] [,3]
>>[1,]4   10   18
>>[2,]   63   64   63
>>[3,]   18   104
>>[,1] [,2] [,3]
>>[1,]4   10   18
>>[2,]   63   64   63
>>[3,]   18   104[,1] [,2] [,3]
>>[1,]4   10   18
>>[2,]   63   64   63
>>[3,]   18   104
>>[,1] [,2] [,3]
>>[1,]4   10   18
>>[2,]   63   64   63
>>[3,]   18   104
>>
>>
>>
>>
>>
>>On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:
>>
>>Hi,
>>>You could try:
>>>
>>>A<- matrix(unlist(read.table(text="
>>>1 2 3
>>>4 5 6
>>>7 8 9
>>>9 8 7
>>>6 5 4
>>>3 2 1
>>>",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
>>>
>>>library(matrixStats)
>>> 
>>>res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
>>> res1
>>>#  [,1] [,2] [,3]
>>>#1    4   10   18
>>>#2   63   64   63
>>>#3   18   10    4
>>>
>>>
>>> 
>>>res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
>>> identical(res1,res2)
>>>#[1] TRUE
>>>
>>>#or
>>> t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x) 
>>>apply(x,2,prod)))
>>>
>>>#or
>>>library(plyr)
>>> 
>>>as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
>>># V1 V2 V3
>>>#[1,]  4 10 18
>>>#[2,] 63 64 63
>>>#[3,] 18 10  4
>>>
>>>#or
>>>do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
>>> colProds(A[x,])))
>>>#or
>>>A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
>>> aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
>>>#  X1 X2 X3
>>>#1  4 10 18
>>>#2 63 64 63
>>>#3 18 10  4
>>>
>>>#or
>>>library(data.table)
>>>At<- data.table(A1,key='ID')
>>>subset(At[,lapply(.SD,colProds),by=ID],select=-1)
>>>#   X1 X2 X3
>>>#1:  4 10 18
>>>#2: 63 64 63
>>>#3: 18 10  4
>>>
>>>A.K.
>>>
>>>
>>>
>>>
>>>Hello,
>>>
>>>I have this matrix :
>>>A =
>>>1 2 3
>>>4 5 6
>>>7 8 9
>>>9 8 7
>>>6 5 4
>>>3 2 1
>>>
>>>I would like to have this matrix (product of rows 2 by 2) :
>>>A =
>>>4 10 18
>>>63 64 63
>>>18 10 4
>>>
>>>Is it possible to do that without a loop ?
>>>
>>>Thank you in advanc

Re: [R] Product of certain rows in a matrix

2013-09-02 Thread arun


HI,
In my first solutions:
 n<-3
 
t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
#  [,1] [,2] [,3]
#1   28   80  162
#2  162   80   28
#3    1    3    5
 n<-4
 
t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),n,nrow(Anew,colProds))
#  [,1] [,2] [,3]
#1  252  640 1134
#2   18   30   20

A.K.

From: Edouard Hardy 
To: arun  
Cc: Bert Gunter ; R help  
Sent: Monday, September 2, 2013 11:46 AM
Subject: Re: [R] Product of certain rows in a matrix



Thank you all for your responses.
The real problem is that all your answer work for products 2 by 2.
I now have to do the product n by n row.
Do you have a solution ?
Thank you in advance,
E.H. 



Edouard Hardy



On Mon, Sep 2, 2013 at 5:43 PM, arun  wrote:

I guess in such situations,
>
>
>fun1<- function(mat){
> if(nrow(mat)%%2==0){
> j<- 2*seq_len(nrow(mat)/2)
> b<- mat[j,]* mat[j-1,]
> }
> else {mat1<- mat[-nrow(mat),]
> j<- 2*seq_len(nrow(mat1)/2)
> b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
>  }
>b
>}
>fun1(A)
># [,1] [,2] [,3]
>
>#[1,]    4   10   18
>#[2,]   63   64   63
>#[3,]   18   10    4
> fun1(Anew)
># [,1] [,2] [,3]
>
>#[1,]    4   10   18
>#[2,]   63   64   63
>#[3,]   18   10    4
>#[4,]    1    3    5
>
>
>A.K.
>
>
>
>
>- Original Message -
>From: arun 
>To: Bert Gunter 
>Cc: R help 
>
>Sent: Monday, September 2, 2013 11:26 AM
>Subject: Re: [R] Product of certain rows in a matrix
>
>Hi Bert,
>Thanks.  It is a better solution.
>
>If nrow() is not even.
>
>Anew<- rbind(A,c(1,3,5))
>j<-seq_len(nrow(Anew)/2)###
> Anew[j,]*Anew[j-1,]
>#Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays
>
>t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
>  [,1] [,2] [,3]
>1    4   10   18
>2   63   64   63
>3   18   10    4
>4    1    3    5
>
>A.K.
>
>
>
>
>
>
>
>From: Bert Gunter 
>To: arun 
>Cc: R help 
>Sent: Monday, September 2, 2013 10:55 AM
>Subject: Re: [R] Product of certain rows in a matrix
>
>
>
>These elaborate manipulations are unnecessary and inefficient. Use indexing 
>instead:
>
>j <- 2*seq_len(nrow(A)/2)
>b <- A[j,]*A[j-1,]
>b
>[,1] [,2] [,3]
>[1,]    4   10   18
>[2,]   63   64   63
>[3,]   18   10    4
>
>[,1] [,2] [,3]
>[1,]4   10   18
>[2,]   63   64   63
>[3,]   18   104
>[,1] [,2] [,3]
>[1,]4   10   18
>[2,]   63   64   63
>[3,]   18   104[,1] [,2] [,3]
>[1,]4   10   18
>[2,]   63   64   63
>[3,]   18   104
>[,1] [,2] [,3]
>[1,]4   10   18
>[2,]   63   64   63
>[3,]   18   104
>
>
>
>
>
>On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:
>
>Hi,
>>You could try:
>>
>>A<- matrix(unlist(read.table(text="
>>1 2 3
>>4 5 6
>>7 8 9
>>9 8 7
>>6 5 4
>>3 2 1
>>",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
>>
>>library(matrixStats)
>> res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
>> res1
>>#  [,1] [,2] [,3]
>>#1    4   10   18
>>#2   63   64   63
>>#3   18   10    4
>>
>>
>> 
>>res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
>> identical(res1,res2)
>>#[1] TRUE
>>
>>#or
>> t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x) 
>>apply(x,2,prod)))
>>
>>#or
>>library(plyr)
>> 
>>as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
>># V1 V2 V3
>>#[1,]  4 10 18
>>#[2,] 63 64 63
>>#[3,] 18 10  4
>>
>>#or
>>do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
>> colProds(A[x,])))
>>#or
>>A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
>> aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
>>#  X1 X2 X3
>>#1  4 10 18
>>#2 63 64 63
>>#3 18 10  4
>>
>>#or
>>library(data.table)
>>At<- data.table(A1,key='ID')
>>subset(At[,lapply(.SD,colProds),by=ID],select=-1)
>>#   X1 X2 X3
>>#1:  4 10 18
>>#2: 63 64 63
>>#3: 18 10  4
>>
>>A.K.
>>
>>
>>
>>
>>Hello,
>>
>>I have this matrix :
>>A =
>>1 2 3
>>4 5 6
>>7 8 9
>>9 8 7
>>6 5 4
>>3 2 1
>>
>>I would like to have this matrix (product of rows 2 by 2) :
>>A =
>>4 10 18
>>63 64 63
>>18 10 4
>>
>>Is it possible to do that without a loop ?
>>
>>Thank you in advance !
>>
>>__
>>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.
>>
>
>
>--
>
>
>Bert Gunter
>Genentech Nonclinical Biostatistics
>
>Internal Contact Info:
>Phone: 467-7374
>Website:
>
>http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
>
>__
>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] Sweave: printing an underscore in the output from an R command

2013-09-02 Thread ONKELINX, Thierry
You have to escape the underscore

\Sexpr{gsub("_", "\_", print(version$platform))}

Best regards,

Thierry


Van: r-help-boun...@r-project.org [r-help-boun...@r-project.org] namens David 
Epstein [david.epst...@warwick.ac.uk]
Verzonden: maandag 2 september 2013 17:38
Aan: r-help@r-project.org
Onderwerp: [R] Sweave: printing an underscore in the output from an R command

I am working with Sweave and would like to print out into my latex document the 
result of the R command
version$platform
So what I first tried in my .Rnw document was \Sexpr{print(version$platform)}.

However, the output from this command is the string "x86_64-apple-darwin10.8.0" 
(without the quotes). This contains an underscore, which is a special character 
in tex and so I get an error message from latex.

I can get round this by using sub to replace underscore with a space, but I 
would like to know how to print the underscore if I really wanted to do so.

__
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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en 
binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is 
door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the 
writer and may not be regarded as stating an official position of INBO, as long 
as the message is not confirmed by a duly signed document.

__
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] Product of certain rows in a matrix

2013-09-02 Thread arun
I guess in such situations,


fun1<- function(mat){
 if(nrow(mat)%%2==0){
 j<- 2*seq_len(nrow(mat)/2)
 b<- mat[j,]* mat[j-1,]
 }
 else {mat1<- mat[-nrow(mat),]
 j<- 2*seq_len(nrow(mat1)/2)
 b<- rbind(mat1[j,]*mat1[j-1,],mat[nrow(mat),])
  }
b
}
fun1(A)
# [,1] [,2] [,3]
#[1,]    4   10   18
#[2,]   63   64   63
#[3,]   18   10    4
 fun1(Anew)
# [,1] [,2] [,3]
#[1,]    4   10   18
#[2,]   63   64   63
#[3,]   18   10    4
#[4,]    1    3    5


A.K.



- Original Message -
From: arun 
To: Bert Gunter 
Cc: R help 
Sent: Monday, September 2, 2013 11:26 AM
Subject: Re: [R] Product of certain rows in a matrix

Hi Bert,
Thanks.  It is a better solution.

If nrow() is not even.

Anew<- rbind(A,c(1,3,5))
j<-seq_len(nrow(Anew)/2)###
 Anew[j,]*Anew[j-1,]
#Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays

t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
  [,1] [,2] [,3]
1    4   10   18
2   63   64   63
3   18   10    4
4    1    3    5

A.K.







From: Bert Gunter 
To: arun  
Cc: R help  
Sent: Monday, September 2, 2013 10:55 AM
Subject: Re: [R] Product of certain rows in a matrix



These elaborate manipulations are unnecessary and inefficient. Use indexing 
instead:

j <- 2*seq_len(nrow(A)/2)
b <- A[j,]*A[j-1,]
b
[,1] [,2] [,3]
[1,]    4   10   18
[2,]   63   64   63
[3,]   18   10    4

[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104
[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104
[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104





On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:

Hi,
>You could try:
>
>A<- matrix(unlist(read.table(text="
>1 2 3
>4 5 6
>7 8 9
>9 8 7
>6 5 4
>3 2 1
>",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
>
>library(matrixStats)
> res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
> res1
>#  [,1] [,2] [,3]
>#1    4   10   18
>#2   63   64   63
>#3   18   10    4
>
>
> res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
> identical(res1,res2)
>#[1] TRUE
>
>#or
> t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x) 
>apply(x,2,prod)))
>
>#or
>library(plyr)
> 
>as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
># V1 V2 V3
>#[1,]  4 10 18
>#[2,] 63 64 63
>#[3,] 18 10  4
>
>#or
>do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
> colProds(A[x,])))
>#or
>A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
> aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
>#  X1 X2 X3
>#1  4 10 18
>#2 63 64 63
>#3 18 10  4
>
>#or
>library(data.table)
>At<- data.table(A1,key='ID')
>subset(At[,lapply(.SD,colProds),by=ID],select=-1)
>#   X1 X2 X3
>#1:  4 10 18
>#2: 63 64 63
>#3: 18 10  4
>
>A.K.
>
>
>
>
>Hello,
>
>I have this matrix :
>A =
>1 2 3
>4 5 6
>7 8 9
>9 8 7
>6 5 4
>3 2 1
>
>I would like to have this matrix (product of rows 2 by 2) :
>A =
>4 10 18
>63 64 63
>18 10 4
>
>Is it possible to do that without a loop ?
>
>Thank you in advance !
>
>__
>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.
>


-- 


Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:

http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] Meaning of "Integer,19"

2013-09-02 Thread David Epstein
I tried example('apply'). Among the various examples, there was the following:

apply> z <- array(1:24, dim = 2:4)
apply> zseq <- apply(z, 1:2, function(x) seq_len(max(x)))
apply> zseq ## a 2 x 3 matrix
 [,1]   [,2]   [,3]
[1,] Integer,19 Integer,21 Integer,23
[2,] Integer,20 Integer,22 Integer,24

The entry "Integer,19" seems to mean the list of integers [1:19], though I'm 
just guessing. Possibly it means some list of 19 integers.

Questions:
Is the notation "Integer,19" documented somewhere? I can't find it.
How might one proceed to find out the meaning of this notation if one didn't 
know it before?
The actual substance of my question is a request for advice on how, in general, 
to look for documentation in R.

I know about help.start(), help.search() and RSiteSearch(). Also, I know how to 
search the archives of r-help. Are there other methods of searching R that I 
should try?
Usually I get more hits than I can cope with.
Thanks










[[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.


[R] Sweave: printing an underscore in the output from an R command

2013-09-02 Thread David Epstein
I am working with Sweave and would like to print out into my latex document the 
result of the R command
version$platform
So what I first tried in my .Rnw document was \Sexpr{print(version$platform)}.

However, the output from this command is the string "x86_64-apple-darwin10.8.0" 
(without the quotes). This contains an underscore, which is a special character 
in tex and so I get an error message from latex.

I can get round this by using sub to replace underscore with a space, but I 
would like to know how to print the underscore if I really wanted to do so.

__
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] Product of certain rows in a matrix

2013-09-02 Thread arun
Hi Bert,
Thanks.  It is a better solution.

If nrow() is not even.

Anew<- rbind(A,c(1,3,5))
j<-seq_len(nrow(Anew)/2)###
 Anew[j,]*Anew[j-1,]
#Error in Anew[j, ] * Anew[j - 1, ] : non-conformable arrays

t(sapply(split(as.data.frame(Anew),as.numeric(gl(nrow(Anew),2,7))),colProds))
  [,1] [,2] [,3]
1    4   10   18
2   63   64   63
3   18   10    4
4    1    3    5

A.K.







From: Bert Gunter 
To: arun  
Cc: R help  
Sent: Monday, September 2, 2013 10:55 AM
Subject: Re: [R] Product of certain rows in a matrix



These elaborate manipulations are unnecessary and inefficient. Use indexing 
instead:

j <- 2*seq_len(nrow(A)/2)
b <- A[j,]*A[j-1,]
b
[,1] [,2] [,3]
[1,]    4   10   18
[2,]   63   64   63
[3,]   18   10    4

[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104
[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104
[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104





On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:

Hi,
>You could try:
>
>A<- matrix(unlist(read.table(text="
>1 2 3
>4 5 6
>7 8 9
>9 8 7
>6 5 4
>3 2 1
>",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
>
>library(matrixStats)
> res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
> res1
>#  [,1] [,2] [,3]
>#1    4   10   18
>#2   63   64   63
>#3   18   10    4
>
>
> res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
> identical(res1,res2)
>#[1] TRUE
>
>#or
> t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x) 
>apply(x,2,prod)))
>
>#or
>library(plyr)
> 
>as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
># V1 V2 V3
>#[1,]  4 10 18
>#[2,] 63 64 63
>#[3,] 18 10  4
>
>#or
>do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
> colProds(A[x,])))
>#or
>A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
> aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
>#  X1 X2 X3
>#1  4 10 18
>#2 63 64 63
>#3 18 10  4
>
>#or
>library(data.table)
>At<- data.table(A1,key='ID')
>subset(At[,lapply(.SD,colProds),by=ID],select=-1)
>#   X1 X2 X3
>#1:  4 10 18
>#2: 63 64 63
>#3: 18 10  4
>
>A.K.
>
>
>
>
>Hello,
>
>I have this matrix :
>A =
>1 2 3
>4 5 6
>7 8 9
>9 8 7
>6 5 4
>3 2 1
>
>I would like to have this matrix (product of rows 2 by 2) :
>A =
>4 10 18
>63 64 63
>18 10 4
>
>Is it possible to do that without a loop ?
>
>Thank you in advance !
>
>__
>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.
>


-- 


Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:

http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

__
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] Multivariate discrete HMMs

2013-09-02 Thread Claus O'Rourke
Hi r-help,

I have been using your RHmm package for some time and have recently
had to try using the package for a new dataset.

Basically I have a dataset with a number of discrete observation
variables that change over time, and I would love to try modeling them
using a HMM.

Basically I was wondering if RHmm can be used to model a multivariate
discrete HMM, i.e., the observations are a vector of discrete
measurements? From what I see in the documentation and from playing
around with examples, it seems like this may not be possible. My
understand of the mathematics behind multivariate HMMs is limited, so
I would appreciate any advance you might be able to give.

Thanks for any help anyone can give

__
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] Product of certain rows in a matrix

2013-09-02 Thread Bert Gunter
These elaborate manipulations are unnecessary and inefficient. Use indexing
instead:

j <- 2*seq_len(nrow(A)/2)
b <- A[j,]*A[j-1,]
b
[,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104

 [,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104


 [,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104

 [,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104


 [,1] [,2] [,3]
[1,]4   10   18
[2,]   63   64   63
[3,]   18   104





On Mon, Sep 2, 2013 at 7:25 AM, arun  wrote:

> Hi,
> You could try:
>
> A<- matrix(unlist(read.table(text="
> 1 2 3
> 4 5 6
> 7 8 9
> 9 8 7
> 6 5 4
> 3 2 1
> ",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)
>
> library(matrixStats)
>
>  res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
>  res1
> #  [,1] [,2] [,3]
> #14   10   18
> #2   63   64   63
> #3   18   104
>
>
>
>  
> res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds))
>  identical(res1,res2)
> #[1] TRUE
>
> #or
>  t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x)
> apply(x,2,prod)))
>
> #or
> library(plyr)
>
>  
> as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
> # V1 V2 V3
> #[1,]  4 10 18
> #[2,] 63 64 63
> #[3,] 18 10  4
>
> #or
> do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
> colProds(A[x,])))
> #or
> A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
>  aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
> #  X1 X2 X3
> #1  4 10 18
> #2 63 64 63
> #3 18 10  4
>
> #or
> library(data.table)
> At<- data.table(A1,key='ID')
> subset(At[,lapply(.SD,colProds),by=ID],select=-1)
> #   X1 X2 X3
> #1:  4 10 18
> #2: 63 64 63
> #3: 18 10  4
>
> A.K.
>
>
>
>
> Hello,
>
> I have this matrix :
> A =
> 1 2 3
> 4 5 6
> 7 8 9
> 9 8 7
> 6 5 4
> 3 2 1
>
> I would like to have this matrix (product of rows 2 by 2) :
> A =
> 4 10 18
> 63 64 63
> 18 10 4
>
> Is it possible to do that without a loop ?
>
> Thank you in advance !
>
> __
> 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.
>



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

[[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] How to catch errors regarding the hessian in 'optim'

2013-09-02 Thread Prof J C Nash (U30A)
This may be one of the many mysteries of the internals of L-BFGS-B, 
which I have found fails from time to time. That is one of the reasons 
for Rvmmin and Rcgmin (and hopefully sooner rather than later Rtn - a 
truncated Newton method, currently working for unconstrained problems, 
but still glitchy for bounds constraints). These are all-R codes so that 
users and developers can get inside to control special situations.


If you have a test problem -- the infamous reproducible example -- there 
are several of us who can likely help to sort out your troubles.


JN


On 13-09-02 06:00 AM, r-help-requ...@r-project.org wrote:

Message: 10
Date: Sun, 1 Sep 2013 17:09:35 +0200
From: Simon Zehnder
To: R-help help
Subject: [R] How to catch errors regarding the hessian in 'optim'
Message-ID:
Content-Type: text/plain; charset=us-ascii

Dear R-Users and R-Developers,

in a comparison between two different estimation approaches I would like to 
catch errors from optim regarding the hessian matrix.

I use optim with method = "L-BFGS-B" thereby relying on numerical 
differentiation for the hessian matrix. I do know, that the estimation approach that uses 
numerical optimization has sometimes problems with singular hessian matrices and I 
consider it as one of its disadvantages of this method. To show the frequency of such 
problems in my simulation study I have to set 'hessian = TRUE' and to collect the errors 
from optim regarding the hessian.

Now I am a little stucked how I could catch specifically errors from the 
hessian matrix in 'optim'. I do know that such errors are thrown most certainly 
from function 'La_solve' in Lapack.c. Does anyone has an idea how I could solve 
this task (clearly with tryCatch but how to choose only errors for the hessian)?


Best

Simon


__
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] Legend position

2013-09-02 Thread Moshiur Rahman
Dear R-helpers,

I'm trying to combine two box plots having two dependent variables:

var1- Orange.area and

var2- Iridescent.area;

two independent categorical factors (each has two levels - 'High' & 'Low'):

fact1- Quantity

fact2- Quality


the data frame (df) is:

   Quantity Quality Orange.area Iridescent.area
1  HighHigh   8.240  12.550
2  High Low  12.690  10.470
3  HighHigh  10.340   4.350
4  High Low  11.430   8.890
5  HighHigh   6.203   6.809
6   Low Low   1.750   7.040
7   LowHigh   6.060   4.930
8   Low Low   5.630   5.980
9   LowHigh   6.540   5.360
10  Low Low   8.458   4.710



I tried to plot them with the following codes:

library(ggplot2)
library(gridExtra)
## 1st plot
p1 <- qplot(as.factor(Quantity), Orange.area, data=df, geom = "boxplot",
fill= Quality)
p1 <-p1 + geom_boxplot() +scale_fill_manual(values = c("dark grey",
"white"), legend="FALSE")
p1 <- p1 + labs(x="Quantity", y="Orange area")
p1 <- p1 + stat_summary(fun.y = mean, geom = "point", color = "black",
aes(group=Quality))
p1 <- p1 + stat_summary(fun.y = mean, geom = "line", aes(group = Quality))
p1 <- p1  +theme_bw()+
  theme(axis.line = element_line(colour = "black"),
strip.text.x = element_blank(),# remove top level title (high and
low)
strip.background = element_blank(),# remove top level background
panel.grid.major = element_blank(),# remove grid line within the
plot
panel.grid.minor = element_blank(),
panel.border = element_blank())
## 2nd plot
p2 <- qplot(as.factor(Quantity), Iridescent.area, data=df, geom =
"boxplot", fill= Quality)
p2 <-p2 + geom_boxplot() +scale_fill_manual(values = c("dark grey",
"white"))
p2 <- p2 + labs(x="Quantity", y="Iridescent area")
p2 <- p2 + stat_summary(fun.y = mean, geom = "point", color = "black",
aes(group=Quality))
p2 <- p2 + stat_summary(fun.y = mean, geom = "line", aes(group = Quality))
p2 <- p2  +
  theme_bw()+
  theme(axis.line = element_line(colour = "black"),
strip.text.x = element_blank(),# remove top level title (high and
low)
strip.background = element_blank(),# remove top level background
panel.grid.major = element_blank(),# remove grid line within the
plot
panel.grid.minor = element_blank(),
panel.border = element_blank())
## combining two plots as one plot
sidebysideplot <- grid.arrange(p1, p2, ncol=2)

and got the plot which sueezed the 2nd plot having legend on its middle of
the right side.

But I want to have the legend on the top right corner with any change of
the 2nd plot (both plots should be the same size).

Is there any help? Any effort will be appreciated...

Cheers,

Moshi
-- 
MD. MOSHIUR RAHMAN
PhD Candidate
School of Animal Biology/Zoology (M092)
University of Western Australia
35 Stirling Hwy, Crawley, WA, 6009
Australia.
Mob.: 061-425205507

[[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] Product of certain rows in a matrix

2013-09-02 Thread arun
Hi,
You could try:

A<- matrix(unlist(read.table(text="
1 2 3
4 5 6
7 8 9
9 8 7
6 5 4
3 2 1
",sep="",header=FALSE)),ncol=3,byrow=FALSE,dimnames=NULL)

library(matrixStats)
 res1<-t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),colProds))
 res1
#  [,1] [,2] [,3]
#1    4   10   18
#2   63   64   63
#3   18   10    4


 res2<-t(sapply(split(as.data.frame(A),((seq_len(nrow(A))-1)%/%2)+1),colProds)) 
 identical(res1,res2)
#[1] TRUE

#or
 t(sapply(split(as.data.frame(A),as.numeric(gl(nrow(A),2,6))),function(x) 
apply(x,2,prod)))

#or
library(plyr)
 as.matrix(ddply(as.data.frame(A),.(as.numeric(gl(nrow(A),2,6))),colProds)[,-1])
# V1 V2 V3
#[1,]  4 10 18
#[2,] 63 64 63
#[3,] 18 10  4

#or
do.call(rbind,tapply(seq_len(nrow(A)),list(as.numeric(gl(nrow(A),2,6))),FUN=function(x)
 colProds(A[x,])))
#or
A1<- data.frame(A,ID=as.numeric(gl(nrow(At),2,6)))
 aggregate(A1[,-4],list(A1[,4]),colProds)[,-1]
#  X1 X2 X3
#1  4 10 18
#2 63 64 63
#3 18 10  4

#or
library(data.table)
At<- data.table(A1,key='ID')
subset(At[,lapply(.SD,colProds),by=ID],select=-1)
#   X1 X2 X3
#1:  4 10 18
#2: 63 64 63
#3: 18 10  4

A.K. 




Hello, 

I have this matrix : 
A = 
1 2 3 
4 5 6 
7 8 9 
9 8 7 
6 5 4 
3 2 1 

I would like to have this matrix (product of rows 2 by 2) : 
A = 
4 10 18 
63 64 63 
18 10 4 

Is it possible to do that without a loop ? 

Thank you in advance !

__
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] Ordering a matrix (and not losing the rownames)

2013-09-02 Thread arun
Hi  Ramón,

It is for the column index.
For ex:
tags_totals[order(tags_totals[,1],decreasing=TRUE),1,drop=FALSE] #same as 
previous solution as there is only one column.  
#   [,1]
#Grupos   23
#Wikis    15
#Glosarios    11
#Bases de datos    7
#Taller    5

order(tags_totals[,1],decreasing=TRUE) #creates the row index

#If you don't specify the column index, it will select all the columns.


 tags_totals[1,1] 
#Wikis 
 #  15 
tags_totals[1,1,drop=FALSE] 
#  [,1]
#Wikis   15

tags_totals[1,,drop=FALSE] 
#  [,1]
#Wikis   15


is.matrix(tags_totals[1,1])
#[1] FALSE
is.matrix(tags_totals[1,1,drop=FALSE])
#[1] TRUE



A.K.


Many thanks Arun, 
This is important for me because I will need to do this operation 
many times. However, there is one thing that intrigues me. Why it is 
necessary to put two commas in a row? 

> tags_totals[order(tags_totals[,1],decreasing=TRUE),,drop=FALSE] 
               [,1] 
Grupos           23 
Wikis            15 
Glosarios        11 
Bases de datos    7 
Taller            5 
> tags_totals[order(tags_totals[,1],decreasing=TRUE),drop=FALSE] 
[1] 23 15 11  7  5 

I've been reading around trrying to undestand it, but I can't see the logic. 

Many thanks 


- Original Message -
From: arun 
To: R help 
Cc: 
Sent: Friday, August 30, 2013 9:27 AM
Subject: Re: Ordering a matrix (and not losing the rownames)

Hi Ramón,
May be this helps:
tags_totals<-matrix(c(15,11,23,7,5),ncol=1,dimnames=list(c("Wikis","Glosarios","Grupos","Bases
 de datos","Taller"),NULL))

tags_totals[order(tags_totals[,1],decreasing=TRUE),,drop=FALSE]
#   [,1]
#Grupos   23
#Wikis    15
#Glosarios    11
#Bases de datos    7
#Taller    5

A.K.



Hello, 

I have a matrix like this: 

> tags_totals 
               [,1] 
Wikis            15 
Glosarios        11 
Grupos           23 
Bases de datos    7 
Taller            5 

And I want to order by the value of the first column. I do this: 

ordered_matrix <- 
as.matrix(tags_totals[order(tags_totals[,1],decreasing=TRUE)]) 

It orders alright, but I lose the rownames, that I need for the graphics 
> ordered_matrix 
     [,1] 
[1,]   23 
[2,]   15 
[3,]   11 
[4,]    7 
[5,]    5 

> rownames(ordered_matrix) 
NULL 

If I try to do it after converting to a dataframe I get an error that I don't 
understand: 

> tags_totals_frame <- as.data.frame(tags_totals) 
> tags_totals_frame[,1] 
[1] 15 11 23  7  5 
> ordered_frame <- 
> tags_totals_frame[order(tags_totals_frame[,1],decreasing=TRUE)] 
Error en `[.data.frame`(tags_totals_frame, order(tags_totals_frame[, 1],  : 
  undefined columns selected 

Thanks on any help, 

-- 
== 
Ramón Ovelar 
Campus Virtual Birtuala UPV/EHU 
Tel: (34) 94 601 3407 
http://campusvirtual.ehu.es


__
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] package seriation- how to manage font size and label margin

2013-09-02 Thread Suparna Mitra
Hello R experts,
 I am trying to use hmap from package seriation or bertinplot.
I have two questions:
How can I specify smaller font? I tried with
pushViewport(viewport(layout=grid.layout(nrow = 1, ncol = 2),
+ gp = gpar(fontsize = 8)))
but didn't work for the font with bertinplot.

Also for hmap I need to arrange the margin so that all the labels are
visible.
I tried with par(mai=c(3,2,2,2)), but also with no luck.
Any help will be really great.
Thanks a lot,
Mitra.

[[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.