![]() |
Introduction
To R |
> person <- list(age=21,name='Fred',score=c(65,78,55)) > person $age: [1] 21 $name: [1] "Fred" $score: [1] 65 78 55The list is constructed with the list() function. The components of the list are introduced using the usual name=arg form of function arguments. When you print the list object you are shown each component with its name and value.
You can access each element by its name, preceded by a $:
> person$name [1] "Fred" name > person$score[2] second element of $score [1] 78You can also access each element by number, using double-square brackets:
> person[[1]] [1] 21 > person[[3]] [1] 65 78 55
> tt <- t.test(rnorm(1000,mean=1),rnorm(1000,mean=1.2),var.equal=T)Nothing is printed because the result is being stored in tt - so type its name to print it out:
> tt Standard Two-Sample t-Test data: rnorm(1000, mean = 1) and rnorm(1000, mean = 1.2) t = -4.5493, df = 1998, p-value = 0 alternative hypothesis: true difference in means is not equal to 0 95 percent confidence interval: -0.2923768 -0.1162318 sample estimates: mean of x mean of y 0.9864194 1.190724Now this is what we saw when we did the t-test earlier. Doesn't look much like the list printout we saw earlier? But it is a list:
> is.list(tt) [1] TRUEYou can get the names of components of a list with the names() function - for this t-test object we get:
> names(tt) [1] "statistic" "parameters" "p.value" "conf.int" "estimate" [6] "null.value" "alternative" "method" "data.name"and so we can get at the confidence interval, for example, with:
> tt$conf.int [1] -0.2923768 -0.1162318 attr(, "conf.level"): [1] 0.95This is a vector of length two, which shows us the 95% confidence interval doesn't contain zero. But whats this attr(,"conf.level") stuff? Thats something called an additional attribute of the vector - in this case it tells is what confidence level the interval is calculated at.