[R] incorrect correlation coefficients

Patrick Burns pburns at pburns.seanet.com
Sun Jul 27 19:08:33 CEST 2014


If the argument to the function you
give in the 'by' call were 'z' instead
of 'x', then it would have been easier
to see what was wrong.

You are using 'xx' inside the function
rather than 'x'.  I think you want
something like:

function(z) {cor(z$Data1.MEAN,
     z$Data2.MEAN)}

Pat

On 27/07/2014 08:04, Mateusz Kędzior wrote:
> Hello all,
>
> I strongly believe, that my issue is quite easy to resolve. However, I have
> no idea how to find/debug what is wrong and I would blame myself for
> insufficient knowledge about data tables and some useful functions in R! as
> apply, sapply, lapply.
>
> My question is as follows:
>
> I would like to display correlation coefficients in a table (ideally - with
> p-value, but below there's only Pearson correlation coefficients). However,
> my code produces exactly the same values for each period (so something is
> obviously wrong). Could you give me any advice:
>      #first of all, I read my data table from CSV file:
>      imported <- read.table (file="/home/someone/data_for_R.csv",
> header=TRUE, sep='\t', quote='"\'', dec=',', fill=FALSE, comment.char="#",
> na.strings = "NA", nrows = -1, skip = 0, check.names = TRUE, strip.white =
> FALSE, blank.lines.skip = TRUE)
>
>      # Typing: class(imported[["Period"]]) produces:
>      # [1] "factor"
>
>      #Typing: levels(imported[["Period"]]) produces:
>      # [1] "Summer 2010" "Summer 2011" "Winter 2010" "Winter 2011" "Winter
> 2012"
>
>      xx <- imported[c("Period","Data1.MEAN","Data2.MEAN")]
>      result <- by(xx, xx$Period, function(x) {cor(xx$Data1.MEAN,
> xx$Data2.MEAN)})
>      result.dataframe <- as.data.frame(as.matrix(result))
>      result.dataframe$C <- rownames(result)
>
>
> Best regards,
> Mateusz
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Patrick Burns
pburns at pburns.seanet.com
twitter: @burnsstat @portfolioprobe
http://www.portfolioprobe.com/blog
http://www.burns-stat.com
(home of:
  'Impatient R'
  'The R Inferno'
  'Tao Te Programming')



More information about the R-help mailing list