Re: [R] For loop with multiple iteration indexes

2018-09-11 Thread David L Carlson
: Monday, September 10, 2018 8:33 PM To: r-help@r-project.org Subject: Re: [R] For loop with multiple iteration indexes Thank you everyone. After thinking about each response, I realized a fairly simple solution is available (obviously, other suggested approaches work as well): stopifnot(length(x

Re: [R] For loop with multiple iteration indexes

2018-09-10 Thread David Disabato
Thank you everyone. After thinking about each response, I realized a fairly simple solution is available (obviously, other suggested approaches work as well): stopifnot(length(x) == length(y); stopifnot(length(x) > 0) r <- list() for (i in 1:length(x) ) { r[[i]] <- cor(x = dat[, x[i] ], y =

Re: [R] For loop with multiple iteration indexes

2018-09-10 Thread Berry, Charles
I have a sense of deja vu: https://www.mail-archive.com/r-help@r-project.org/msg250494.html There is some good advice there. > On Sep 9, 2018, at 3:49 PM, David Disabato wrote: > > Hi R-help, > > I am trying to create a for loop with multiple iteration indexes. I don't > want to use two

Re: [R] For loop with multiple iteration indexes

2018-09-10 Thread Albrecht Kauffmann
Hi, this simple example is very similarly, and it works in R: r <- list() n <- 0 x <- c("a","b","c")#x,y: Data from a dataframe y <- c("A","B","C") for (k in 1:3) { n <- n+1 r[[n]] <- paste0(x[k],y[k])#or any other function using x[k] and y[k] as arguments } print(r) Is it this what

Re: [R] For loop with multiple iteration indexes

2018-09-09 Thread Jim Lemon
Hi David, If you mean that you have two data frames named x and y and want the correlations between the columns that would be on the diagonal of a correlation matrix: r<-list() for(i in 1:n) r[[i]]<-cor(x[,i],y[,i]) If I'm wrong, let me know. Jim On Mon, Sep 10, 2018 at 3:06 PM David Disabato

[R] For loop with multiple iteration indexes

2018-09-09 Thread David Disabato
Hi R-help, I am trying to create a for loop with multiple iteration indexes. I don't want to use two different for loops nested together because I don't need the full matrix of the two indexes, just the diagonal elements (e.g., i[1] & j[1] and i[2] & j[2], but not i[1] & j[2]). Is there a way to