rcmoulirc
rcmoulirc

Reputation: 33

Sorting data in R and how to extract values?

This is my dataset and I am new to R and iam trying to write a script for this data set.

R> head(KenTau)
  Age CapReg TrSw FeelChk CanSw
1  20      1    0       0     0
2  36      1    0       0     0
3  35      1    3       2     2
4  21      0    0       2     2
5  43      0    0       2     2
6  34      1    0       0     0 

I want to compare TrSw with rest of the colmn variable i.e

TrSw Vs Age
TrSw Vs CapReg  
TrSw Vs FeelChk 
TrSw Vs CanSw

I use this one to run it on R and I use this command

cor.test(KenTau$Age, KenTau$TrSw, alternative="two.sided", method="kendall")

also I want to extract Age and pvalue so I can have a list as I have close to 50 variables.

dput() of data:

KenTau <- structure(list(Age = c(20L, 36L, 35L, 21L, 43L, 34L, 37L, 62L, 
54L, 47L, 48L, 45L, 2L, 2L, 2L, 54L, 52L, 40L, 58L, 29L, 27L, 
28L, 46L, 35L, 50L, 31L, 48L, 2L, 29L, 54L, 52L, 28L, 28L, 26L, 
38L, 59L, 51L, 58L, 39L, 44L, 53L, 2L, 39L, 55L, 48L, 2L, 23L, 
51L, 50L, 26L, 28L, 40L, 38L, 61L, 52L, 33L, 2L, 59L, 27L, 45L, 
45L, 57L, 66L, 52L, 58L, 34L, 28L, 39L, 48L, 53L, 39L, 46L, 57L, 
36L, 25L, 22L, 29L, 46L, 25L, 25L, 35L, 44L, 24L, 26L, 33L, 27L, 
41L, 28L, 26L, 32L, 36L, 35L, 32L, 33L, 29L, 29L, 52L, 55L, 23L, 
29L, 45L, 26L, 48L, 54L, 50L, 35L, 27L, 39L, 41L, 30L, 30L, 31L, 
27L, 28L, 27L, 25L, 34L, 23L, 30L, 34L, 52L, 20L, 31L, 2L, 45L, 
34L, 21L, 60L, 34L, 40L, 47L, 30L, 54L, 36L, 32L, 31L, 55L, 57L, 
23L, 31L, 26L, 26L, 27L, 19L, 26L, 25L, 37L, 47L, 38L, 38L, 26L, 
25L, 41L), CapReg = c(1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
1L,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 2L, 
    3L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 0L, 1L, 0L, 2L, 1L, 3L, 1L, 
    0L, 0L, 2L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 1L, 2L, 0L, 1L, 2L, 
    2L, 1L, 1L, 0L, 2L, 0L, 0L, 1L, 0L, 0L, 2L, 1L, 0L, 0L, 0L, 
    0L, 2L, 1L, 2L, 0L, 2L, 2L, 0L, 1L, 2L, 0L, 1L, 0L, 2L, 2L, 
    2L, 0L, 0L, 2L, 3L, 2L, 0L, 0L, 2L, 2L, 2L, 2L, 2L, 2L, 0L, 
    0L, 0L, 2L, 2L, 1L, 1L, 2L, 1L, 0L, 0L, 2L, 0L, 1L, 2L, 2L, 
    1L, 1L, 0L, 0L, 2L, 2L, 0L, 2L, 2L, 3L, 1L, 1L, 0L, 2L, 0L, 
    2L, 0L)), .Names = c("Age", "CapReg", "TrSw", "FeelChk", 
"CanSw"), class = "data.frame", row.names = c(NA, -153L))

Upvotes: 0

Views: 231

Answers (1)

Gavin Simpson
Gavin Simpson

Reputation: 174778

Whilst I'm not convinced of the statistical merits of generating the p-values for 50 correlations, this is quite easy to do with lapply() and friends.

For this I chose to iterate over the indices of names of KenTau that are not "TrSw" as that is the variable you wish to compare all others with. I first grab those indices using which():

R> inds <- which(names(KenTau) != "TrSw")
R> inds
[1] 1 2 4 5

Next I set up a call to lapply(), where I will iterate over inds. I now need an anonymous function that takes an index ind as the first argument (this is what lapply() will pass my function at each iteration), and I need to pass in the data, which I do so as argument x. My anonymous function calls cor.test() as you show in your example, but notice how x[, ind] is used to refer to the current index or column we are correlating with TrSw. The last part of the lapply() call says to pass as x, the data KenTau so that whenever you see x in the anonymous function this really refers to a copy of KenTau:

cors <- lapply(inds,
               function(ind, x) {
                   cor.test(x[, ind], x[, "TrSw"], alternative="two.sided",
                            method="kendall")
               }, x = KenTau)

Adding some names to the list that is cors will help later so do that now:

names(cors) <- names(KenTau)[inds]

If we look at cors we see that it is a list:

R> str(cors, max = 1)
List of 4
 $ Age    :List of 8
  ..- attr(*, "class")= chr "htest"
 $ CapReg :List of 8
  ..- attr(*, "class")= chr "htest"
 $ FeelChk:List of 8
  ..- attr(*, "class")= chr "htest"
 $ CanSw  :List of 8
  ..- attr(*, "class")= chr "htest"

each element of the list being an object of class "htest", which is what cor.test() returns. There are four such objects because there were four variables to compare with TrSw.

You wish to extract the p-value, so we need to see where this is stored in an "htest" object:

R> str(cors[[1]])
List of 8
 $ statistic  : Named num 1.57
  ..- attr(*, "names")= chr "z"
 $ parameter  : NULL
 $ p.value    : num 0.116
 $ estimate   : Named num 0.105
  ..- attr(*, "names")= chr "tau"
 $ null.value : Named num 0
  ..- attr(*, "names")= chr "tau"
 $ alternative: chr "two.sided"
 $ method     : chr "Kendall's rank correlation tau"
 $ data.name  : chr "x[, ind] and x[, \"TrSw\"]"
 - attr(*, "class")= chr "htest"

The above output shows the p-value is stored in component p.value. To extract all 4 p-values, we want to, in effect, do this:

 res[[i]][["p.value"]]

where i is each element of cors in turn. For this we could use lapply() again, but sapply() will simplify the result to a vector for us, which is neater in this case. The sapply() call will pass us each res[[i]] turn, so we just need to apply the [[ function (yes, it may not look like one, but it very much is a function; "[["()). That function takes a single argument (in this case we can use the name of the component we want to extract), which I pass in as "p.value":

res <- sapply(cors, `[[`, "p.value")

Because I added names to cors, sapply() will return a named vector containing the p-values of the correlation between the named variable an TrSw:

R> res
         Age       CapReg      FeelChk        CanSw 
1.157889e-01 3.920115e-01 2.189736e-04 1.578040e-06

If you want another component of the result, say the test statistic itself, then replace "p.value" with the name of the component you want, e.g. "statistic" to get Kendall's Tau.

If you are going to be doing this for a lot of variables, go and read about multiple tests and adjusting p-values as I'm not convinced your results would be that useful as just 50 correlations.

Upvotes: 3

Related Questions