arun
2013-Oct-09 13:26 UTC
[R] Correlating data after changing one observation for one variable
Hi,
trees <-? structure(list(Girth = c(8.3, 8.6, 8.8, 10.5, 10.7, 10.8, 11,
11, 11.1, 11.2, 11.3, 11.4, 11.4, 11.7, 12, 12.9, 12.9, 13.3,
13.7, 13.8, 14, 14.2, 14.5, 16, 16.3, 17.3, 17.5, 17.9, 18, 18,
20.6), Height = c(70L, 65L, 63L, 72L, 81L, 83L, 66L, 75L, 80L,
75L, 79L, 76L, 76L, 69L, 75L, 74L, 85L, 86L, 71L, 64L, 78L, 80L,
74L, 72L, 77L, 81L, 82L, 80L, 80L, 80L, 87L), Volume = c(10.3,
10.3, 10.2, 16.4, 18.8, 19.7, 15.6, 18.2, 22.6, 19.9, 24.2, 21,
21.4, 21.3, 19.1, 22.2, 33.8, 27.4, 25.7, 24.9, 34.5, 31.7, 36.3,
38.3, 42.6, 55.4, 55.7, 58.3, 51.5, 51, 77)), .Names = c("Girth",
"Height", "Volume"), row.names = c("1",
"2", "3", "4", "5", "6",
"7", "8", "9", "10", "11",
"12", "13", "14", "15", "16",
"17",
"18", "19", "20", "21", "22",
"23", "24", "25", "26", "27",
"28",
"29", "30", "31"), class = "data.frame")
with(trees,cor(Volume,Height))
#[1] 0.5982497
?with(trees,cor(Volume,Girth))
#[1] 0.9671194
?trees$Volume[31]<- 35
with(trees,cor(Volume,Girth))
#[1] 0.9012941
?with(trees,cor(Volume,Height))
[1] 0.5298093
#Looks like you ?attach() trees
attach(trees)
?cor(Volume,Height)
#[1] 0.5298093
?cor(Volume,Girth)
#[1] 0.9012941
trees[31,3] <- 77
You need to attach() again
attach(trees)
?cor(Volume,Height)
#[1] 0.5982497
?cor(Volume,Girth)
#[1] 0.9671194
It is better not to use ?attach().? Try using ?with()
A.K.
After having changed the last observation for Volume from 77.0 to 35,
> trees[31,3]<-35
> trees
? ?Girth Height Volume
1 ? ?8.3 ? ? 70 ? 10.3
2 ? ?8.6 ? ? 65 ? 10.3
3 ? ?8.8 ? ? 63 ? 10.2
4 ? 10.5 ? ? 72 ? 16.4
5 ? 10.7 ? ? 81 ? 18.8
6 ? 10.8 ? ? 83 ? 19.7
7 ? 11.0 ? ? 66 ? 15.6
8 ? 11.0 ? ? 75 ? 18.2
9 ? 11.1 ? ? 80 ? 22.6
10 ?11.2 ? ? 75 ? 19.9
11 ?11.3 ? ? 79 ? 24.2
12 ?11.4 ? ? 76 ? 21.0
13 ?11.4 ? ? 76 ? 21.4
14 ?11.7 ? ? 69 ? 21.3
15 ?12.0 ? ? 75 ? 19.1
16 ?12.9 ? ? 74 ? 22.2
17 ?12.9 ? ? 85 ? 33.8
18 ?13.3 ? ? 86 ? 27.4
19 ?13.7 ? ? 71 ? 25.7
20 ?13.8 ? ? 64 ? 24.9
21 ?14.0 ? ? 78 ? 34.5
22 ?14.2 ? ? 80 ? 31.7
23 ?14.5 ? ? 74 ? 36.3
24 ?16.0 ? ? 72 ? 38.3
25 ?16.3 ? ? 77 ? 42.6
26 ?17.3 ? ? 81 ? 55.4
27 ?17.5 ? ? 82 ? 55.7
28 ?17.9 ? ? 80 ? 58.3
29 ?18.0 ? ? 80 ? 51.5
30 ?18.0 ? ? 80 ? 51.0
31 ?20.6 ? ? 87 ? 35.0
the correlation coefficient does not change, which it should...
earlier: > cor(Volume, Girth)
[1] 0.9671194 > cor(Volume, Height)
[1] 0.5982497
and after changing value: > cor(Volume, Girth)
[1] 0.9671194 > cor(Volume, Height)
[1] 0.5982497
Why is this?
Thanks in advance ;)
Victoria