Select columns in different dataframes using indexes and loop

Hi, i'm dealing with time-histories included in two different data-frames. Both dataframes contain the same number of time-series (say N columns). I need to select the first column of dataframe1 in order to convolve it with the N-th column of the dataframe2.
So i was hoping to work by using indexes to select the desidered columns in each data-frame .
Here the code i try, obtaining messages as follows
Error in fft(x) non-numeric argument
**1: Unknown or uninitialised column: chd. *
*2: Unknown or uninitialised column: chr.

I'm rather new to R, i was using VBA for long time in the past, did i miss some basic rule in R?

# Rename columns
i <- 0
for (i in 0:49){
  nome <-paste("ch",i,sep = "")
  colnames(dataframe1)[i]=nome
  colnames(dataframe2)[i]=nome
  }
#Apply convolution to selected columns
d <-1
r <-49
i <-0
for(i in 1:49){
d <- d+1
r <- r-1
i <- i+1
chd <-paste("ch",d,sep= "")
chr <-paste("ch",r,sep= "")
sh1 <-dataframe1$chd
sh2 <-dataframe2$chr
sg(i)=convolve(sh1,sh2,type="circular")
}

two points
a)
you cant generate a name and access that in a data.frame column with $ syntax; that would require [[ ]] syntax

chd <-paste("ch",d,sep= "")
chr <-paste("ch",r,sep= "")
sh1 <-dataframe1[[chd]]
sh2 <-dataframe2[[chr]]

b)
when your loop starts the first d and r are 2 and 28 respectively, and the last are 50 and 0 .

see for yourself >

d <-1
r <-49
i <-0
for(i in 1:49){
  d <- d+1
  r <- r-1
  i <- i+1
  cat("\n",d," ",r)
}

Thanks a lot Nir, now i see that columns are properly selected and it works.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.