[R] using a for loop with variable as vectors

baptiste auguie ba208 at exeter.ac.uk
Fri Feb 27 21:23:05 CET 2009


Hi,

you could do one of the following,


1) combine a, b, c, d, e in a list and use ?lapply

my.list <- list(a,b,c,d,e)

lapply(my.list, foo)

where foo() is a function to be applied to each individual element


2) alternatively, see ?get to retrieve the value of a variable from  
its name. Your loop would read,

for ( r in c("a", "b")) {

t.test (y , get(r))

}

(untested)

Hope this helps,

baptiste

On 27 Feb 2009, at 20:12, Laura Lucia Prieto Godino wrote:

> Dear R users,
> I am completelly lost with the following:
>
> I have the following vectors a, b ,c, d and e
> + a
> [1] 279.3413 268.0450 266.3062 433.8438 305.4650 317.4712 288.3413
> 374.6950
>>
>> b
>  [1] 170.4500 254.5675 219.5762 232.3425 200.2738 238.2637 210.6062
> 262.4825 345.2387 269.3763
> [11] 190.1225 259.7750 241.1350 265.8775 175.4162 206.4238 202.1738
> 151.1550 213.9900 225.5825
>> c
>  [1] 278.6150 356.7738 426.5113 444.9538 335.2850 354.1463 526.6487
> 476.7912 503.7763 614.5300
> [11] 504.4400 367.5537 485.6862 497.9550
>> d
> [1] 476.6212 525.3800 405.4200 372.8800
>> e
>  [1] 428.4762 383.6587 184.6238 343.4737 306.7475 364.2300 441.0150
> 428.0275 389.0713 285.2125
> [11] 396.0612 403.4787 375.4900 416.3575 543.3388 454.2475 430.1313
>
>
> and I want to compare them in pair for significance with a t-test, so
> I wrote 2 for loops one inside the other, so that the variable of the
> for loop each of the for loop would be one of my vectors at a time, I
> wrote it like this
>
> #Test for normality
> shapiro.test(y)
>
> #Get the variables accessible.
> shapiro.test(y)->out
> attach(out)
> p.value
>
> #Perform a normal T-test if data is normal and a non-parametric one
> when the data are not normaly distributed
> for (r in expression(a,b,c,d,e)) {
>
> {
> if (p.value>0.05)
>        {
>        print(y)
>        print("T-Test_normal")
>        print(paste("Today is", date()))
>        print(t.test(y,r,alternative="greater"))
>
>
>        }
>
> else {
>
>        print("T-Test_wilcoxon")
>        print(paste("Today is", date()))
>        print(wilcox.test(y,r,alternative="greater"))
>
>        }
> }
> }
> }
>
> But somehow it doesnt work,because it considers a,b,c,d,e as strings
> and not as the vectors I have defined, and I have no idea how to make
> a for loop in which it loops takes a variable. Can anyone help me? I
> am lost
>
> Thank you!
>
> Lucia
>        [[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.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag




More information about the R-help mailing list