Re: [R] how to loop through variables in R?

2010-11-23 Thread Ivan Calandra

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, wata...@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@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.calan...@uni-hamburg.de

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

__
R-help@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.


Re: [R] how to loop through variables in R?

2010-11-22 Thread watashi


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@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.


Re: [R] how to loop through variables in R?

2010-11-22 Thread watashi
-Original Message-

From: Yuliya Matveyeva 
To: wata...@post.com
Sent: Mon, Nov 22, 2010 4:32 pm
Subject: Re: [R] how to loop through variables in R?


If you want to have a name-specific loop.
Assign names to your variables after inserting them into the data.frame like 
that:
colnames(df) <- c("var1","var23","var456","var44",...)
for (nam in colnames(df)) {
 myfunction(df[[nam]])
}
Data.frames support access by names.


Unfortunately, in this sense I have to type in 1000 times... isn't there any 
function that allows retrieving and assigning all columns automatically? 
read.table in "the R intro manual" just brief says it can read a table but then 
there's no follow up about how to access the data

 

[[alternative HTML version deleted]]

__
R-help@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.


Re: [R] how to loop through variables in R?

2010-11-22 Thread Jeff Newmiller

wata...@post.com wrote:

-Original Message-
From: jas4710 
To: r-help@r-project.org
Sent: Mon, Nov 22, 2010 4:11 pm
Subject: [R] how to loop through variables in R?




After importing a table with "M" variables and "N" records, I'd like to

calculate chi-square statistics, say, between N1, N2; N1, N3, ..., N1, Ni,

and then N2, N3, ... N2, Ni, ..., Ni-1, Ni.  Two loops should be ok but the

manual & online help don't show a systematic way to do so but instead show

hard-code examples so users have to type in the names themselves one by

one...

 Sorry for typo.  It should be "N" variables and "M" records. 


Thank you very much.
  
Assuming you are working with a data frame df, and your variable with 
the name of a column in it is col1, you should be able to extract that 
column as a vector using df[[col1]]. And yes, "the manual" does describe 
this notation.


__
R-help@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.


Re: [R] how to loop through variables in R?

2010-11-22 Thread watashi


-Original Message-
From: jas4710 
To: r-help@r-project.org
Sent: Mon, Nov 22, 2010 4:11 pm
Subject: [R] how to loop through variables in R?




After importing a table with "M" variables and "N" records, I'd like to

calculate chi-square statistics, say, between N1, N2; N1, N3, ..., N1, Ni,

and then N2, N3, ... N2, Ni, ..., Ni-1, Ni.  Two loops should be ok but the

manual & online help don't show a systematic way to do so but instead show

hard-code examples so users have to type in the names themselves one by

one...



-- 



 Sorry for typo.  It should be "N" variables and "M" records. 

Thank you very much.
 

 



 

[[alternative HTML version deleted]]

__
R-help@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.


[R] how to loop through variables in R?

2010-11-22 Thread jas4710

After importing a table with "M" variables and "N" records, I'd like to
calculate chi-square statistics, say, between N1, N2; N1, N3, ..., N1, Ni,
and then N2, N3, ... N2, Ni, ..., Ni-1, Ni.  Two loops should be ok but the
manual & online help don't show a systematic way to do so but instead show
hard-code examples so users have to type in the names themselves one by
one...

-- 
View this message in context: 
http://r.789695.n4.nabble.com/how-to-loop-through-variables-in-R-tp3053214p3053214.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.