Re: [R] A small nag

2011-08-14 Thread padmanabhan . vijayan
Is your column dimension of file(,3:10)  9?
Your dim(LGD) is 8
That could be the problem.
Regards
VIjayan Padmanabhan
Sent from my BlackBerry® smartphone from !DEA

-Original Message-
From: Chintanu 
Sender: r-help-bounces@r-project.orgDate: Mon, 15 Aug 2011 13:41:13 
To: Joshua Wiley
Cc: 
Subject: Re: [R] A small nag

Hello Joshua,

I could feel that my explanation was bad so far. Now, giving another effort
here to simplify things :

I have a dataframe ("file") containing 8 samples (in columns). Those
samples' results (numericals) are available in the dataframe's rows.

LGD is another vector.

LGD <- c(11.6, 12.3, 15.8, 33.1, 43.5, 51.3, 67.3, 84.9)

Now, correlation needs to be found between -

i) each of the rows of the dataframe, and
 ii) LGD

Thanks,
Chintanu



===

On Mon, Aug 15, 2011 at 1:12 PM, Joshua Wiley wrote:

> On Sun, Aug 14, 2011 at 7:21 PM, Chintanu  wrote:
> > Hi Joshua,
> >
> > SORRY for not making that clear. I wish to have the correlation values
> > between each column of my "file" with the "LGD". For example:
> >
> > cor (Column 1, LGD)
> > cor (column 2, LGD) ... so on.
>
> Okay, you need to make a tractable example.  Create or give us data
> where cor(Column1, LGD) works.  LGD is a vector of length 8, file is
> probably some sort of matrix or data frame, which you are extracting
> part of, but there are way too many possible ways to repeat,
> transpose, twist, and otherwise manipulate the data into some sort of
> correlatable form (using rep() is not sufficient---that just gives you
> a really long vector).
>
> If you are currently under the impression that it is possible to
> correlate a 47231 x 1 matrix with a vector of length 8, read the
> Wikipedia page so you understand how correlation works:
> http://en.wikipedia.org/wiki/Correlation_and_dependence.
>
> >
> > The first one you have provided is producing an error :
> >
> >> sapply(file[1:47231, 3:10], FUN = cor, y = rep(LGD, 47231), method =
> >> "pearson")
> > Error in FUN(X[[1L]], ...) : incompatible dimensions
> > Cheers,
> > Chintanu
> >
> >
> > ===
> >
> > On Mon, Aug 15, 2011 at 12:09 PM, Joshua Wiley 
> > wrote:
> >>
> >> Hi Chintanu,
> >>
> >> Do you want the correlation of columns 3:10 of file with the y vector
> >> or do you want a correlation matrix of all variables?
> >>
> >> ## correlation between cols 3:10 and y
> >> sapply(file[1:47231, 3:10], FUN = cor, y = rep(LGD, 47231), method =
> >> "pearson")
> >>
> >> ## correlation matrix
> >> cor(cbind(file[1:47231, 3:10], rep(LGD, 47231)), method = "pearson")
> >>
> >> HTH,
> >>
> >> Josh
> >>
> >>
> >> On Sun, Aug 14, 2011 at 7:02 PM, Chintanu  wrote:
> >> > Hi,
> >> >
> >> > I am not sure how to fix the following error.
> >> >
> >> > LGD <-  c(11.6,   12.3,  15.8,  33.1,  43.5,  51.3,
> >> > 67.3,  84.9)
> >> >
> >> > cor (x=(file [1:47231,3:10]), y= rep (LGD, 47231), method = "pearson")
> >> >
> >> > Error in cor(x = (file[1:47231, 3:10]), y = rep(LGD, 47231), method =
> >> > "pearson") :
> >> >
> >> >  incompatible dimensions
> >> >
> >> >> sessionInfo()
> >> >
> >> > R version 2.13.0 (2011-04-13)
> >> >
> >> > Platform: i386-pc-mingw32/i386 (32-bit)
> >> >
> >> > locale:
> >> >
> >> > [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252
> >> >   LC_MONETARY=English_Australia.1252
> >> > LC_NUMERIC=C   LC_TIME=English_Australia.1252
> >> >
> >> > attached base packages:
> >> >
> >> > [1] stats graphics  grDevices utils datasets  methods   base
> >> >
> >> > loaded via a namespace (and not attached):
> >> >
> >> > [1] tools_2.13.0
> >> >
> >> > Thank you.
> >> >
> >> > Kind regards,
> >> >
> >> > Chintanu
> >> >
> >> >[[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.
> >> >
> >>
> >>
> >>
> >> --
> >> Joshua Wiley
> >> Ph.D. Student, Health Psychology
> >> Programmer Analyst II, ATS Statistical Consulting Group
> >> University of California, Los Angeles
> >> https://joshuawiley.com/
> >
> >
>
>
>
> --
>  Joshua Wiley
> Ph.D. Student, Health Psychology
> Programmer Analyst II, ATS Statistical Consulting Group
> University of California, Los Angeles
> https://joshuawiley.com/
>

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

Re: [R] A small nag

2011-08-14 Thread padmanabhan . vijayan
For your second question.. Why not make a function on your own using 
if(),is.vector(), is.matrix(),is.dataframe() and club it to return different 
values accordingly?
Regards
VP
Sent from my BlackBerry® smartphone from !DEA

-Original Message-
From: Chintanu 
Date: Mon, 15 Aug 2011 15:03:43 
To: Joshua Wiley
Cc: ; 
Subject: Re: [R] A small nag

Hello Josh,

Thank you - that worked. Also, thanks to VIjayan Padmanabhan for your
effort.

Further, please allow me to ask 2 quick questions:

1. The default "cor" takes Pearson correlation. How would I change it to,
say *Spearman* correlation ? Something like the following doesn't work.

> apply(file[1, 3:10], 1, cor(method = "spearman"), y = LGD)
Error in .Internal(inherits(x, what, which)) : 'x' is missing


2. Is there any function available that can be used to check whether a
variable is vector, matrix or dataframe ?

Thank you.

regards,
Chintanu


=

On Mon, Aug 15, 2011 at 2:03 PM, Joshua Wiley wrote:

> On Sun, Aug 14, 2011 at 8:41 PM, Chintanu  wrote:
> > Hello Joshua,
> >
> > I could feel that my explanation was bad so far. Now, giving another
> effort
> > here to simplify things :
> >
> > I have a dataframe ("file") containing 8 samples (in columns). Those
> > samples' results (numericals) are available in the dataframe's rows.
> >
> > LGD is another vector.
> >
> > LGD <- c(11.6, 12.3, 15.8, 33.1, 43.5, 51.3, 67.3, 84.9)
> >
> > Now, correlation needs to be found between -
> >
> > i) each of the rows of the dataframe, and
> > ii) LGD
>
> Ah, rows, then try:
>
> apply(file[1:47321, 3:10], 1, cor, y = LGD)
>
> it is basically an implicit for loop that loops through the first
> argument (your file matrix), row by row, correlating each row with
> LGD.  So:
>
> cor(file[1, 3:10], LGD)
>
> from 1 to 47321. It is not very efficient, but even on my slow laptop
> it is a matter of seconds so speed is probably not a big issue unless
> it is part of a simulation or something.  I have a sense that a smidge
> of clever work with matrices could avoid the apply() call, but its not
> jumping out at me.
>
> Cheers!
>
> Josh
>
> > Thanks,
> > Chintanu
> >
> >
> >
> > ===
> >
> > On Mon, Aug 15, 2011 at 1:12 PM, Joshua Wiley 
> > wrote:
> >>
> >> On Sun, Aug 14, 2011 at 7:21 PM, Chintanu  wrote:
> >> > Hi Joshua,
> >> >
> >> > SORRY for not making that clear. I wish to have the correlation values
> >> > between each column of my "file" with the "LGD". For example:
> >> >
> >> > cor (Column 1, LGD)
> >> > cor (column 2, LGD) ... so on.
> >>
> >> Okay, you need to make a tractable example.  Create or give us data
> >> where cor(Column1, LGD) works.  LGD is a vector of length 8, file is
> >> probably some sort of matrix or data frame, which you are extracting
> >> part of, but there are way too many possible ways to repeat,
> >> transpose, twist, and otherwise manipulate the data into some sort of
> >> correlatable form (using rep() is not sufficient---that just gives you
> >> a really long vector).
> >>
> >> If you are currently under the impression that it is possible to
> >> correlate a 47231 x 1 matrix with a vector of length 8, read the
> >> Wikipedia page so you understand how correlation works:
> >> http://en.wikipedia.org/wiki/Correlation_and_dependence.
> >>
> >> >
> >> > The first one you have provided is producing an error :
> >> >
> >> >> sapply(file[1:47231, 3:10], FUN = cor, y = rep(LGD, 47231), method =
> >> >> "pearson")
> >> > Error in FUN(X[[1L]], ...) : incompatible dimensions
> >> > Cheers,
> >> > Chintanu
> >> >
> >> >
> >> > ===
> >> >
> >> > On Mon, Aug 15, 2011 at 12:09 PM, Joshua Wiley <
> jwiley.ps...@gmail.com>
> >> > wrote:
> >> >>
> >> >> Hi Chintanu,
> >> >>
> >> >> Do you want the correlation of columns 3:10 of file with the y vector
> >> >> or do you want a correlation matrix of all variables?
> >> >>
> >> >> ## correlation between cols 3:10 and y
> >> >> sapply(file[1:47231, 3:10], FUN = cor, y = rep(LGD, 47231), method =
> >> >> "pearson")
> >> >>
> >> >> ## correlation matrix
> >> >> cor(cbind(file[1:47231, 3:10], rep(LGD, 47231)), method = "pearson")
> >> >>
> >> >> HTH,
> >> >>
> >> >> Josh
> >> >>
> >> >>
> >> >> On Sun, Aug 14, 2011 at 7:02 PM, Chintanu 
> wrote:
> >> >> > Hi,
> >> >> >
> >> >> > I am not sure how to fix the following error.
> >> >> >
> >> >> > LGD <-  c(11.6,   12.3,  15.8,  33.1,  43.5,  51.3,
> >> >> > 67.3,  84.9)
> >> >> >
> >> >> > cor (x=(file [1:47231,3:10]), y= rep (LGD, 47231), method =
> >> >> > "pearson")
> >> >> >
> >> >> > Error in cor(x = (file[1:47231, 3:10]), y = rep(LGD, 47231), method
> =
> >> >> > "pearson") :
> >> >> >
> >> >> >  incompatible dimensions
> >> >> >
> >> >> >> sessionInfo()
> >> >> >
> >> >> > R version 2.13.0 (2011-04-13)
> >> >> >
> >> >> > Platform: i386-pc-mingw32/i386 (32-bit)
> >> >> >
> >

Re: [R] FW: Renaming levels of a factor in a dataframe

2011-08-15 Thread padmanabhan . vijayan
Try recode from Deducer package.
Sent from my BlackBerry® smartphone from !DEA

-Original Message-
From: B Jessop 
Sender: r-help-bounces@r-project.orgDate: Mon, 15 Aug 2011 11:36:07 
To: 
Subject: [R] FW:  Renaming levels of a factor in a dataframe


Tyler,

 

Tried your solution: levels (Data1$Site <- list(Fw = c("AB"), Est = c("DE"))) 

 

but still got a NULL response to str(Data1) and an alternating list of Fw, Est, 
Fw, Est under Site when looked at in the Data editor in R console. The use of 
the "levels" function would seem to be appropriate but tricky to use.  Any 
other ideas would be welcome.

 

Regards,

BJ


> From: tyler_rin...@hotmail.com 
> To: deel...@hotmail.com; r-help@r-project.org 
> Subject: RE: [R] Renaming levels of a factor in a dataframe 
> Date: Sun, 14 Aug 2011 13:18:12 -0400 
> 
> Here's an example of relevel used to relevel and combine groups 
> 
> InsectSprays2<-InsectSprays 
> levels(InsectSprays2$spray) 
> levels(InsectSprays2$spray)<-list(new1=c("A","C"),YEPS=c("B","D","E"),LASTLY="F")
>  
> levels(InsectSprays2$spray) 
> InsectSprays2 
> 
> So for you try... 
> levels (Data1$Site) <- list(Fw =c( "AB"), Est = c("DE")) 
> 
> > From: deel...@hotmail.com 
> > To: r-help@r-project.org 
> > Date: Sun, 14 Aug 2011 12:56:25 -0300 
> > Subject: [R] Renaming levels of a factor in a dataframe 
> > 
> > 
> > 
> > Dear Helplist: 
> > 
> > 
> > 
> > I am trying, unsuccessfully, to rename levels of a factor in a 
> dataframe. The dataframe consists of two factor variables and one 
> numeric variable as follows: 
> > 
> > Factor Site has 2 levels AB and DE, factor Fish has 30 levels, 15 
> associated with each Site e.g. 1-1, 1-2,.2-1, 2-2 I am trying 
> to rename the levels of factor Site from AB to Fw and DE to Est while 
> keeping them as factors. The following 2 approaches do not work, each 
> giving a NULL response and creating a character string. 
> > 
> > 
> > 
> > levels (Data1$Site <- c("Fw", "Est")) This simply gives an 
> alternating list of Fw, Est, Fw, Est... not the desired 15 concurrent 
> rows of Fw followed by 15 of Est. 
> > 
> > 
> > 
> > #levels (Data1$Site <- list(Fw = "AB", Est = "DE")) This gives the 
> same result. I have tried other approaches to no avail. It seems a 
> simple problem but has not been so. 
> > 
> > 
> > 
> > Any suggestions for solving this problem would be much appreciated. 
> > 
> > 
> > 
> > Regards, 
> > 
> > BJ 
> > __ 
> > 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] Filtering a table

2011-08-16 Thread padmanabhan . vijayan
Have you tried subset after making your data into a dataframe?
Regards
VP
Sent from my BlackBerry® smartphone from !DEA

-Original Message-
From: Santini Silvana 
Sender: r-help-bounces@r-project.orgDate: Tue, 16 Aug 2011 16:38:20 
To: r-help@r-project.org
Reply-To: Santini Silvana 
Subject: [R] Filtering a table

Hello, I have a big table with 3 columns and 103918 rows. This is the example,

           time             species                 dbh
5 1  4.9377297 
575 1  11.64127213 
575 1  109.8182438 
575 1  8.029809521 
5 1  24.32501874 
575 1  4.895992119 
575 1  11.40567637 
575 1  2.795090562 
575 1  21.79281837 
575 1  52.57476174 
575 1  27.7290919 
575 1  3.23262083 
575 2  19.30612651 
575 1  2.956672964 
575 1  111.690689 
575 1  11.82499086 
575 1  63.86200585 
575 1  111.8312759 
575 1  49.23078501 
25 1  2.810866156 
575 1  10.93097209 
575 1  23.7930745 
575 1  21.68010008 
575 1  13.32423271 
575 1  23.10306499 
575 1  59.646657 
1000 2  20.47707761 
575 1  3.255755538 
575 1  29.3392412 
575 1  2.578551542 
575 1  52.71564453 
575 1  119.8069955 
575 1  83.45738555 
575 1  7.763555744 
725 1  2.578551542 
I would like to calculate the dbh mean but only for species 1, time 575. I have 
tried this

> names (exp1)
[1] "time"    "species" "dbh"    
> time = c(1)
> species=c(2)
> dbh=c(3)
> dbhmean <- mean (exp1 [time==575 & species ==1])
> dbhmean
named list()

I am not sure why it appears the message "named list()"...

Can anybody give me some hints on how to do this correctly?

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.