[R] how to loop through variables in R?

Ivan Calandra ivan.calandra at uni-hamburg.de
Tue Nov 23 09:56:25 CET 2010


Hi!

You haven't got any answer probably because you didn't provide a 
reproducible example. You can do it by copy/pasting the output of 
dput(d) or dput(df).
Moreover, before this email, I couldn't really understand what you were 
trying to do. It's not crystal clear now, but I think I got it.

First, when you read your txt, don't you already have the correct 
data.frame? What is the difference between d and df? It looks like your 
cbind() step is complicated. You can also index columns by their index 
numbers.
So let's say you want in df the columns 1 to 5 and 6 to 8 from d. You 
can do it like this:
sel <- c(1:5,6:8)  ## creates a vector with the columns indexes you want 
to have in df
df <- d[, sel]       ## extract these columns from d and assign it into 
df. Note the first comma!
You can also so it in one step of course:
df <- d[, c(1:5,6:8)]

Second, in your loop, you overwrite at each iteration the result from 
the previous one. You could do something like this:
result <- numeric(length(df))  ## shouldn't it be length(df)-1?
for (i in 1:(length(df)-1)) {
  result[i] <- chisq.test(table(df[[i]], df[[i+1]]))  ## each 
computation will be stored in a different element of result
}


Next, chisq.test() returns a list, so it's not really a good idea to 
store the output in a vector.
Take a look at
str(chisq.test(table(df[[1]], df[[2]])))
to know which element(s) you want to keep.
You would probably want something like this:
chisq.test(table(df[[1]], df[[2]]))[1:3]

So back to your loop!
result <- vector(mode="list", length=length(df))  ## create a list, 
shouldn't it here also be length(df)-1?
names(result) <- paste("chisq_df[[", 1:length(df), "]]_df[[", 
(1:length(df))+1, "]]", sep="") ## that way, your list is named, which 
is easier to remember what is
                                                                         
                                                                         
## what if you have lots of columns
for (i in 1:(length(df)-1)) {
  result[[i]] <- chisq.test(table(df[[i]], df[[i+1]]))[1:3]  ## each 
computation will be stored in a different element of the list
}

Is it what you're looking for?
HTH,
Ivan



Le 11/23/2010 03:11, watashi at post.com a écrit :
>
> d<-read.table("D:\\Working\\Statics.txt")
>
> df<- cbind("Q1", "Q2", "Q3", "Q4", "Q5", "Q5A", "Q5B", "Q5C", "Q5D", "Q5E", "Q5F", "Q5G", "Q6", "Q6A", "Q6B", "Q6C", "Q6D", "Q6E", "Q6F", "Q7", "Q8", "Q9")
> #Than you can loop through them simply by doing:
> result<- numeric(length(df))
> for (i in 1:(length(df)-1)) {
>   result<- chisq.test(table(df[[i]], df[[i+1]]))
> }
>
> and then this error comes out:
>
> Error: unexpected '}' in "}"
>
>
> and how can I redirect the output of the chi-square test to a file instead of console output?
>
>
> 	[[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.
>

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/1525_8_1.php



More information about the R-help mailing list