# Correlation coefficient for data table

I would like to display correlation coefficients in a table (ideally - with p-value). 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)


EDIT:

Code which reads file from Github:

library(RCurl)
x <- getURL("https://raw.githubusercontent.com/kedziorm/testowe/master/data_for_R.csv")
imported <- read.csv (text=x, 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)
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)


EDIT: This should finally work:

x <- "Period\tDate\tData1.MEAN\tData1.MEDIAN\tData2.MEAN\tData2.MEDIAN\tData3.MEAN\tData3.MEDIAN\nWinter 2010\t26-03-2010\t0,3580917\t0,307479\t0,551191\t0,612853\t0,3476462\t0,3996462\nWinter 2010\t26-04-2010\t0,3016958\t0,2643808\t0,417791\t0,393714\t0,2811050286\t0,3061050286\nSummer 2010\t03-07-2010\t0,1916181\t0,1816603\t0,390925\t0,37385\t0,2183438286\t0,2923438286\nSummer 2010\t04-07-2010\t0,2548711\t0,1738567\t0,4349834\t0,4957131\t0,2467746286\t0,3437746286\nWinter 2011\t01-11-2010\t0,3393042\t0,2870481\t0,497295\t0,538132\t0,3210420857\t0,3690420857\nSummer 2011\t04-06-2011\t0,222748\t0,2218226\t0,363823\t0,275725\t0,2309696\t0,2809696\nSummer 2011\t05-06-2011\t0,241889\t0,1918457\t0,373566\t0,292997\t0,2306573429\t0,2966573429\nWinter 2012\t07-11-2011\t0,2264874\t0,2601413\t0,373048\t0,274139\t0,2456219143\t0,2756219143\nWinter 2012\t08-11-2011\t0,2414665\t0,2662565\t0,314382\t0,279857\t0,2348871429\t0,2598871429\nWinter 2012\t09-11-2011\t0,2817838\t0,2325952\t0,376063\t0,468148\t0,254412\t0,287412\nWinter 2012\t10-11-2011\t0,2476841\t0,2667485\t0,406902\t0,476582\t0,2632384571\t0,3632384571\n"
imported <- read.csv (text=x, 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)
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)

• Please include a reproducible example. That is, one that when we run your code, we get what you get. – Glen_b -Reinstate Monica Jul 27 '14 at 9:22
• Sorry for that. I uploaded CSV file to GitHub and change my code. – matandked Jul 29 '14 at 9:28
• code doesn't run. "Error in function (type, msg, asError = TRUE) : SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed" – Glen_b -Reinstate Monica Jul 29 '14 at 9:32
• Why not simply create a small example -- one small enough to run dput on (for example), so you can create the data in your post. So take a subset of your data, dput it and assign the result to x. – Glen_b -Reinstate Monica Jul 29 '14 at 9:35

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

The variable passed in is x, not xx. The function only knows xx because it exists in the parent environment. You pass in the subsetted data but then ignore it in favor of the unsubsetted data. That is, because you reference a variable other than the one by passes it, it evaluates the correlation on the whole data every time.
Replace xx with x there and it looks right to me, though the last line might be superfluous depending on what you're trying to achieve.