I have a list of 8 data frames and when I tried to compute additional columns and append them to each data frame, the results of "cosine" function were with opposites sign can someone helps me to figure out the problem. since my real code is very long, I will write some simple code instead. but briefly, I have to create four columns by multiplying specific columns of the data frame by cos(2*pi/12)and then sin(2*pi/12) and then cos(2*pi/6) and finally by sin(2*pi/6). the problem appears only with the first two i.e. multiplication by cos(2*pi/12)and then sin(2*pi/12).
a<- list(data.frame(T=c(73:171)), data.frame(T=c(73:172)))
val1<- 2*pi/12
val2<- 2*pi/6
a<- lapply(a, function(x) {x$frqy.1<- cos(x$T * val1);return(x)})
a<- lapply(a, function(x) {x$frqy.2<- sin(x$T * val1);return(x)})
a<- lapply(a, function(x) {x$frqy.3<- cos( x$T * val2);return(x)})
a<- lapply(a, function(x) {x$frqy.4<- sin( x$T *val2);return(x)})
Unwanted results are
The input data can be found in the following link 1drv.ms/f/s!Aj6GG03kq2tsgSaOZ5qTsmw1ul2w
#my actual code is
setwd("D:/R Practice/newtimeseries")
filenames <- gsub("\\.csv$","", list.files(pattern="\\.csv$"))
for(j in filenames){
assign(j, read.csv(paste(j, ".csv", sep=""),stringsAsFactors=FALSE))
}
mydf <- mget(ls()[sapply(ls(), function(x) is.data.frame(get(x)))])
library(lubridate)
mylist<- lapply(mydf, function(x) {dmy(x$date); return(x)})
mylist<- lapply(mylist, function(x) {colnames(x)<-c("date","ssh"); x})
mylist<- lapply(mylist, function(x) {x$month<-month(as.POSIXlt(x$date, format="%d-%m-%Y")); return(x)})
mylist<- lapply(mylist, function(x) {x$year<-year(as.POSIXlt(x$date, format="%d-%m-%y")); return(x)})
month_no<- function(y,y0,m,m0){
month_no<-(y-y0)*12+(m-m0)+1
return(month_no)
}
mylist<- lapply(mylist, function(x) {x$a1<-month_no(x$year,2002,x$month,7); return(x)})
val1<- 2*pi/12
val2<- 2*pi/6
mylist<- lapply(mylist, function(x) {x$frqy.1<- cos(x$a1 * val1);return(x)})
mylist<- lapply(mylist, function(x) {x$frqy.2<- sin(x$a1 * val1);return(x)})
mylist<- lapply(mylist, function(x) {x$frqy.3<-cos( x$a1 * val2);return(x)})
mylist<- lapply(mylist, function(x) {x$frqy.4<-sin( x$a1 * val2);return(x)})