-1

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

enter image description here

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)})
Jisika
  • 405
  • 3
  • 9
  • 4
    Please reduce this to something simpler. – G. Grothendieck Sep 03 '19 at 14:19
  • I am just wondering why cosine function gives me a wrong answer. I forgot to mention that my values in radians – Jisika Sep 03 '19 at 15:15
  • If you look at the result the column "frqy.1", and "frqy.2" values should be in the opposite sign. The code that I have written here is working. but when I run my actual code, the wrong values appear – Jisika Sep 03 '19 at 15:28
  • 1
    Well how is your actual code different from the code you've written here? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Sep 03 '19 at 15:34
  • @MrFlick No, The actual code is same but the result is different. when I run my actual input, the error occurs. the data frames that I post here is just random. – Jisika Sep 04 '19 at 05:05
  • If someone can help me, please. I just don't understand why such a problem happened. although in my code I have two cosine functions. one of them working the second not. – Jisika Sep 04 '19 at 06:01
  • To whom tried to help me thanks a lot because of your contribution. Your comments made me reconsider the code repeatedly and this led to the discovery of the source of the mistake, very sorry. The error was not in R but I took a wrong column in lapply function I type (x$month) whereas I should take (x$a1) – Jisika Sep 04 '19 at 13:53

1 Answers1

1

I can not reproduce your results. When I run your code i get:

head(a[[1]], 11)
#    T        frqy.1        frqy.2 frqy.3        frqy.4
#1  73  8.660254e-01  5.000000e-01    0.5  8.660254e-01
#2  74  5.000000e-01  8.660254e-01   -0.5  8.660254e-01
#3  75  6.859879e-15  1.000000e+00   -1.0  1.371976e-14
#4  76 -5.000000e-01  8.660254e-01   -0.5 -8.660254e-01
#5  77 -8.660254e-01  5.000000e-01    0.5 -8.660254e-01
#6  78 -1.000000e+00  5.144755e-15    1.0 -1.028951e-14
#7  79 -8.660254e-01 -5.000000e-01    0.5  8.660254e-01
#8  80 -5.000000e-01 -8.660254e-01   -0.5  8.660254e-01
#9  81 -3.429630e-15 -1.000000e+00   -1.0  6.859260e-15
#10 82  5.000000e-01 -8.660254e-01   -0.5 -8.660254e-01
#11 83  8.660254e-01 -5.000000e-01    0.5 -8.660254e-01

You are not multiplying specific columns of the data frame by cos(2*pi/12) ..., your are calculating cos(73 * 2*pi/12), cos(74 * 2*pi/12), ...

GKi
  • 37,245
  • 2
  • 26
  • 48
  • yeah, this what I am doing. here it works but my actual code doesn't give the correct answer although the steps are the same – Jisika Sep 03 '19 at 15:32
  • what is the problem, is the problem in my Rstudio setup? – Jisika Sep 03 '19 at 15:33
  • 1
    @Jisika Then its hard to find the reason, when it even does not appear in the example in your Question. – GKi Sep 03 '19 at 15:34
  • 2
    Your code isn't following your description. As @GKi said, you are calculating `cos(73 * 2*pi/12)`, not `cos(2*pi/12)*73` as your text suggests you want. – user2554330 Sep 03 '19 at 16:37
  • Yeah, you all are right. my problem is solved thanks for the help. – Jisika Sep 04 '19 at 13:56