문제

Say I have two data sets. Ill call my first set train and here are the variables

month = c(1,1,1,2,2,2,3,3,3,4,4,4)
day=c(3,8,12,3,8,12,3,8,12,3,8,12)
trend=c(0.1,0.2,0.3,0.4,0.5,0.4,0.3,0.2,0.1,0.2,0.3,0.4) 


train=cbind(month,day,trend)

And say my second set is called test, which has month and day variables

tsmonth = c(1,2,2,3,3,3,4,4,4)
tsday=c(3,3,12,3,8,12,3,8,12)

now i want to fill in my trend portion of the test set using values from training data for example: in my test set, January 3rd, had a trend value of 0.1 there the first value in my test should be 0.1 so in the end I should get a

tstrend = 0.1, 0.4, 0.4....so on

I tried to code up something like this but it gave me an error msg and I don't really know what to change here

tstrend=rep(0,length(tsmonth))
for (i in 1:length(tsmonth)){
for (j in 1:length(month)){
if (tsday[i] = day[j] & tsmonth[i] =month[j])
{
tstrend[i] = trend[j]
}
}
}

I would really appreciate all your help.

Thank you, A

도움이 되었습니까?

해결책

I don't quite understand what you are trying to do, but I definetly see something wrong with that if() statement, it should be:

if (tsday[i] == day[j] && tsmonth[i] == month[j])

== to compare = to assign a value

in most languages: boolean expression && another boolean expression -> boolean (true/false) and

ex: true && true -> true

bit array & bit array 2 -> bitwise and

ex: 0011 & 0101 -> 0001

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top