R - Create (Mutate) a new column as a function of past observations












0














Ok so i have a pretty large data set of around 500 observations and 3 variables. The first column refers to time.



For a test data set I am using:



dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.5,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")


Time Var1 Var2
1 1 1.0 2.0
2 2 1.8 4.8
3 3 3.5 6.5
4 4 3.8 8.8
5 5 5.6 10.6
6 6 6.2 12.2
7 7 7.8 14.8
8 8 8.2 16.2
9 9 9.8 18.8
10 10 10.1 20.1


So what I need to do is create a new column that each observations is the slope respect to time of some past points. For example taking 3 past points it would be something like:



slopeVar1[i]=slope(Var1[i-2:i],Time[i-2:i]) #Not real code
slopeVar[i]=slope(Var2[i-2:i],Time[i-2:i]) #Not real code

Time Var1 Var2 slopeVar1 slopeVar2
1 1 1 2 NA NA
2 2 1.8 4.8 NA NA
3 3 3.5 6.5 1.25 2.25
4 4 3.8 8.8 1.00 2.00
5 5 5.6 10.6 1.05 2.05
6 6 6.2 12.2 1.20 1.70
7 7 7.8 14.8 1.10 2.10
8 8 8.2 16.2 1.00 2.00
9 9 9.8 18.8 1.00 2.00
10 10 10.1 20.1 0.95 1.95


I actually got as far as using a for() function, but for really large data sets (>100,000) it starts taking too long.



The for() argument that I used is shown bellow:



#CREATE DATA FRAME
rm(dat)
dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.333,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")
dat
plot(dat)

#CALCULATE SLOPE OF n POINTS FROM i TO i-n.
#In this case I am taking just 3 points, but it should
#be possible to change the number of points taken.

attach(dat)
n=3 #number for points to take slope
l=dim(dat[1])[1] #number of iterations
y=0
x=0
slopeVar1=NA
slopeVar2=NA
for (i in 1:l) {
if (i<n) {slopeVar1[i]=NA} #For the rows where there are not enough previous observations, it outputs NA
if (i>=n) {
y1=Var1[(i-n+1):i] #y data sets for calculating slope of Var1
y2=Var2[(i-n+1):i]#y data sets for calculating slope of Var2
x=Time[(i-n+1):i] #x data sets for calculating slope of Var1&Var2

z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z2[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}
slopeVar1 #Checking results.
slopeVar2

(result=cbind(dat,slopeVar1,slopeVar2)) #Binds original data with new calculated slopes.


This code actually outputs what I want; but again, for really large data sets is quite inefficient.










share|improve this question
























  • "... slope respect to time of some past points ..." could be be precise what exactly you want to calculate when n = 3 for e.g.?
    – Andre Elrico
    Nov 19 '18 at 13:47












  • For eg. how do you get/derive 1.25and 2.25 in the 3rd row?
    – Andre Elrico
    Nov 19 '18 at 13:51










  • you could PROFILE your code and identify bottle necks. I would assume its comes from fitting a bazillion lm. Also google: "r parallel for loop"
    – Andre Elrico
    Nov 19 '18 at 13:56










  • Ok so for example: - slopeVar1[3]=1.25 comes from the linear regresion of Var1[1:3]=[1, 1.8, 3.5] respect to Time[1:3]=[1,2,3].
    – Mario del Pino
    Nov 19 '18 at 18:54


















0














Ok so i have a pretty large data set of around 500 observations and 3 variables. The first column refers to time.



For a test data set I am using:



dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.5,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")


Time Var1 Var2
1 1 1.0 2.0
2 2 1.8 4.8
3 3 3.5 6.5
4 4 3.8 8.8
5 5 5.6 10.6
6 6 6.2 12.2
7 7 7.8 14.8
8 8 8.2 16.2
9 9 9.8 18.8
10 10 10.1 20.1


So what I need to do is create a new column that each observations is the slope respect to time of some past points. For example taking 3 past points it would be something like:



slopeVar1[i]=slope(Var1[i-2:i],Time[i-2:i]) #Not real code
slopeVar[i]=slope(Var2[i-2:i],Time[i-2:i]) #Not real code

Time Var1 Var2 slopeVar1 slopeVar2
1 1 1 2 NA NA
2 2 1.8 4.8 NA NA
3 3 3.5 6.5 1.25 2.25
4 4 3.8 8.8 1.00 2.00
5 5 5.6 10.6 1.05 2.05
6 6 6.2 12.2 1.20 1.70
7 7 7.8 14.8 1.10 2.10
8 8 8.2 16.2 1.00 2.00
9 9 9.8 18.8 1.00 2.00
10 10 10.1 20.1 0.95 1.95


I actually got as far as using a for() function, but for really large data sets (>100,000) it starts taking too long.



The for() argument that I used is shown bellow:



#CREATE DATA FRAME
rm(dat)
dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.333,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")
dat
plot(dat)

#CALCULATE SLOPE OF n POINTS FROM i TO i-n.
#In this case I am taking just 3 points, but it should
#be possible to change the number of points taken.

attach(dat)
n=3 #number for points to take slope
l=dim(dat[1])[1] #number of iterations
y=0
x=0
slopeVar1=NA
slopeVar2=NA
for (i in 1:l) {
if (i<n) {slopeVar1[i]=NA} #For the rows where there are not enough previous observations, it outputs NA
if (i>=n) {
y1=Var1[(i-n+1):i] #y data sets for calculating slope of Var1
y2=Var2[(i-n+1):i]#y data sets for calculating slope of Var2
x=Time[(i-n+1):i] #x data sets for calculating slope of Var1&Var2

z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z2[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}
slopeVar1 #Checking results.
slopeVar2

(result=cbind(dat,slopeVar1,slopeVar2)) #Binds original data with new calculated slopes.


This code actually outputs what I want; but again, for really large data sets is quite inefficient.










share|improve this question
























  • "... slope respect to time of some past points ..." could be be precise what exactly you want to calculate when n = 3 for e.g.?
    – Andre Elrico
    Nov 19 '18 at 13:47












  • For eg. how do you get/derive 1.25and 2.25 in the 3rd row?
    – Andre Elrico
    Nov 19 '18 at 13:51










  • you could PROFILE your code and identify bottle necks. I would assume its comes from fitting a bazillion lm. Also google: "r parallel for loop"
    – Andre Elrico
    Nov 19 '18 at 13:56










  • Ok so for example: - slopeVar1[3]=1.25 comes from the linear regresion of Var1[1:3]=[1, 1.8, 3.5] respect to Time[1:3]=[1,2,3].
    – Mario del Pino
    Nov 19 '18 at 18:54
















0












0








0







Ok so i have a pretty large data set of around 500 observations and 3 variables. The first column refers to time.



For a test data set I am using:



dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.5,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")


Time Var1 Var2
1 1 1.0 2.0
2 2 1.8 4.8
3 3 3.5 6.5
4 4 3.8 8.8
5 5 5.6 10.6
6 6 6.2 12.2
7 7 7.8 14.8
8 8 8.2 16.2
9 9 9.8 18.8
10 10 10.1 20.1


So what I need to do is create a new column that each observations is the slope respect to time of some past points. For example taking 3 past points it would be something like:



slopeVar1[i]=slope(Var1[i-2:i],Time[i-2:i]) #Not real code
slopeVar[i]=slope(Var2[i-2:i],Time[i-2:i]) #Not real code

Time Var1 Var2 slopeVar1 slopeVar2
1 1 1 2 NA NA
2 2 1.8 4.8 NA NA
3 3 3.5 6.5 1.25 2.25
4 4 3.8 8.8 1.00 2.00
5 5 5.6 10.6 1.05 2.05
6 6 6.2 12.2 1.20 1.70
7 7 7.8 14.8 1.10 2.10
8 8 8.2 16.2 1.00 2.00
9 9 9.8 18.8 1.00 2.00
10 10 10.1 20.1 0.95 1.95


I actually got as far as using a for() function, but for really large data sets (>100,000) it starts taking too long.



The for() argument that I used is shown bellow:



#CREATE DATA FRAME
rm(dat)
dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.333,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")
dat
plot(dat)

#CALCULATE SLOPE OF n POINTS FROM i TO i-n.
#In this case I am taking just 3 points, but it should
#be possible to change the number of points taken.

attach(dat)
n=3 #number for points to take slope
l=dim(dat[1])[1] #number of iterations
y=0
x=0
slopeVar1=NA
slopeVar2=NA
for (i in 1:l) {
if (i<n) {slopeVar1[i]=NA} #For the rows where there are not enough previous observations, it outputs NA
if (i>=n) {
y1=Var1[(i-n+1):i] #y data sets for calculating slope of Var1
y2=Var2[(i-n+1):i]#y data sets for calculating slope of Var2
x=Time[(i-n+1):i] #x data sets for calculating slope of Var1&Var2

z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z2[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}
slopeVar1 #Checking results.
slopeVar2

(result=cbind(dat,slopeVar1,slopeVar2)) #Binds original data with new calculated slopes.


This code actually outputs what I want; but again, for really large data sets is quite inefficient.










share|improve this question















Ok so i have a pretty large data set of around 500 observations and 3 variables. The first column refers to time.



For a test data set I am using:



dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.5,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")


Time Var1 Var2
1 1 1.0 2.0
2 2 1.8 4.8
3 3 3.5 6.5
4 4 3.8 8.8
5 5 5.6 10.6
6 6 6.2 12.2
7 7 7.8 14.8
8 8 8.2 16.2
9 9 9.8 18.8
10 10 10.1 20.1


So what I need to do is create a new column that each observations is the slope respect to time of some past points. For example taking 3 past points it would be something like:



slopeVar1[i]=slope(Var1[i-2:i],Time[i-2:i]) #Not real code
slopeVar[i]=slope(Var2[i-2:i],Time[i-2:i]) #Not real code

Time Var1 Var2 slopeVar1 slopeVar2
1 1 1 2 NA NA
2 2 1.8 4.8 NA NA
3 3 3.5 6.5 1.25 2.25
4 4 3.8 8.8 1.00 2.00
5 5 5.6 10.6 1.05 2.05
6 6 6.2 12.2 1.20 1.70
7 7 7.8 14.8 1.10 2.10
8 8 8.2 16.2 1.00 2.00
9 9 9.8 18.8 1.00 2.00
10 10 10.1 20.1 0.95 1.95


I actually got as far as using a for() function, but for really large data sets (>100,000) it starts taking too long.



The for() argument that I used is shown bellow:



#CREATE DATA FRAME
rm(dat)
dat=as.data.frame(matrix(c(1,2,3,4,5,6,7,8,9,10,
1,1.8,3.333,3.8,5.6,6.2,7.8,8.2,9.8,10.1,
2,4.8,6.5,8.8,10.6,12.2,14.8,16.2,18.8,20.1),10,3))
colnames(dat)=c("Time","Var1","Var2")
dat
plot(dat)

#CALCULATE SLOPE OF n POINTS FROM i TO i-n.
#In this case I am taking just 3 points, but it should
#be possible to change the number of points taken.

attach(dat)
n=3 #number for points to take slope
l=dim(dat[1])[1] #number of iterations
y=0
x=0
slopeVar1=NA
slopeVar2=NA
for (i in 1:l) {
if (i<n) {slopeVar1[i]=NA} #For the rows where there are not enough previous observations, it outputs NA
if (i>=n) {
y1=Var1[(i-n+1):i] #y data sets for calculating slope of Var1
y2=Var2[(i-n+1):i]#y data sets for calculating slope of Var2
x=Time[(i-n+1):i] #x data sets for calculating slope of Var1&Var2

z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z2[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}
slopeVar1 #Checking results.
slopeVar2

(result=cbind(dat,slopeVar1,slopeVar2)) #Binds original data with new calculated slopes.


This code actually outputs what I want; but again, for really large data sets is quite inefficient.







r dataframe calculated-columns mutate






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 18:52







Mario del Pino

















asked Nov 19 '18 at 13:39









Mario del PinoMario del Pino

33




33












  • "... slope respect to time of some past points ..." could be be precise what exactly you want to calculate when n = 3 for e.g.?
    – Andre Elrico
    Nov 19 '18 at 13:47












  • For eg. how do you get/derive 1.25and 2.25 in the 3rd row?
    – Andre Elrico
    Nov 19 '18 at 13:51










  • you could PROFILE your code and identify bottle necks. I would assume its comes from fitting a bazillion lm. Also google: "r parallel for loop"
    – Andre Elrico
    Nov 19 '18 at 13:56










  • Ok so for example: - slopeVar1[3]=1.25 comes from the linear regresion of Var1[1:3]=[1, 1.8, 3.5] respect to Time[1:3]=[1,2,3].
    – Mario del Pino
    Nov 19 '18 at 18:54




















  • "... slope respect to time of some past points ..." could be be precise what exactly you want to calculate when n = 3 for e.g.?
    – Andre Elrico
    Nov 19 '18 at 13:47












  • For eg. how do you get/derive 1.25and 2.25 in the 3rd row?
    – Andre Elrico
    Nov 19 '18 at 13:51










  • you could PROFILE your code and identify bottle necks. I would assume its comes from fitting a bazillion lm. Also google: "r parallel for loop"
    – Andre Elrico
    Nov 19 '18 at 13:56










  • Ok so for example: - slopeVar1[3]=1.25 comes from the linear regresion of Var1[1:3]=[1, 1.8, 3.5] respect to Time[1:3]=[1,2,3].
    – Mario del Pino
    Nov 19 '18 at 18:54


















"... slope respect to time of some past points ..." could be be precise what exactly you want to calculate when n = 3 for e.g.?
– Andre Elrico
Nov 19 '18 at 13:47






"... slope respect to time of some past points ..." could be be precise what exactly you want to calculate when n = 3 for e.g.?
– Andre Elrico
Nov 19 '18 at 13:47














For eg. how do you get/derive 1.25and 2.25 in the 3rd row?
– Andre Elrico
Nov 19 '18 at 13:51




For eg. how do you get/derive 1.25and 2.25 in the 3rd row?
– Andre Elrico
Nov 19 '18 at 13:51












you could PROFILE your code and identify bottle necks. I would assume its comes from fitting a bazillion lm. Also google: "r parallel for loop"
– Andre Elrico
Nov 19 '18 at 13:56




you could PROFILE your code and identify bottle necks. I would assume its comes from fitting a bazillion lm. Also google: "r parallel for loop"
– Andre Elrico
Nov 19 '18 at 13:56












Ok so for example: - slopeVar1[3]=1.25 comes from the linear regresion of Var1[1:3]=[1, 1.8, 3.5] respect to Time[1:3]=[1,2,3].
– Mario del Pino
Nov 19 '18 at 18:54






Ok so for example: - slopeVar1[3]=1.25 comes from the linear regresion of Var1[1:3]=[1, 1.8, 3.5] respect to Time[1:3]=[1,2,3].
– Mario del Pino
Nov 19 '18 at 18:54














1 Answer
1






active

oldest

votes


















1














This quick rollapply implemenation seems to be speeding it up somewhat -



library("zoo")
slope_func = function(period) {
y1=period[,2] #y data sets for calculating slope of Var1
y2=period[,3] #y data sets for calculating slope of Var2
x=period[,1] #x data sets for calculating slope of Var1&Var2
z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z1[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}

start = Sys.time()
rollapply(dat[1:3], FUN=slope_func, width=3, by.column=FALSE)
end=Sys.time()
print(end-start)

Time difference of 0.04980111 secs


OP's previous implementation was taking Time difference of 0.2666121 secs for the same






share|improve this answer





















  • How would I get the slope for Var2?
    – Mario del Pino
    Nov 19 '18 at 19:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375869%2fr-create-mutate-a-new-column-as-a-function-of-past-observations%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This quick rollapply implemenation seems to be speeding it up somewhat -



library("zoo")
slope_func = function(period) {
y1=period[,2] #y data sets for calculating slope of Var1
y2=period[,3] #y data sets for calculating slope of Var2
x=period[,1] #x data sets for calculating slope of Var1&Var2
z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z1[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}

start = Sys.time()
rollapply(dat[1:3], FUN=slope_func, width=3, by.column=FALSE)
end=Sys.time()
print(end-start)

Time difference of 0.04980111 secs


OP's previous implementation was taking Time difference of 0.2666121 secs for the same






share|improve this answer





















  • How would I get the slope for Var2?
    – Mario del Pino
    Nov 19 '18 at 19:20
















1














This quick rollapply implemenation seems to be speeding it up somewhat -



library("zoo")
slope_func = function(period) {
y1=period[,2] #y data sets for calculating slope of Var1
y2=period[,3] #y data sets for calculating slope of Var2
x=period[,1] #x data sets for calculating slope of Var1&Var2
z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z1[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}

start = Sys.time()
rollapply(dat[1:3], FUN=slope_func, width=3, by.column=FALSE)
end=Sys.time()
print(end-start)

Time difference of 0.04980111 secs


OP's previous implementation was taking Time difference of 0.2666121 secs for the same






share|improve this answer





















  • How would I get the slope for Var2?
    – Mario del Pino
    Nov 19 '18 at 19:20














1












1








1






This quick rollapply implemenation seems to be speeding it up somewhat -



library("zoo")
slope_func = function(period) {
y1=period[,2] #y data sets for calculating slope of Var1
y2=period[,3] #y data sets for calculating slope of Var2
x=period[,1] #x data sets for calculating slope of Var1&Var2
z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z1[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}

start = Sys.time()
rollapply(dat[1:3], FUN=slope_func, width=3, by.column=FALSE)
end=Sys.time()
print(end-start)

Time difference of 0.04980111 secs


OP's previous implementation was taking Time difference of 0.2666121 secs for the same






share|improve this answer












This quick rollapply implemenation seems to be speeding it up somewhat -



library("zoo")
slope_func = function(period) {
y1=period[,2] #y data sets for calculating slope of Var1
y2=period[,3] #y data sets for calculating slope of Var2
x=period[,1] #x data sets for calculating slope of Var1&Var2
z1=lm(y1~x) #Temporal value of slope of Var1
z2=lm(y2~x) #Temporal value of slope of Var2
slope1=as.data.frame(z1[1]) #Temporal value of slope of Var1
slopeVar1[i]=slope1[2,1] #Populating string of slopeVar1
slope2=as.data.frame(z1[1])#Temporal value of slope of Var2
slopeVar2[i]=slope2[2,1] #Populating string of slopeVar2
}
}

start = Sys.time()
rollapply(dat[1:3], FUN=slope_func, width=3, by.column=FALSE)
end=Sys.time()
print(end-start)

Time difference of 0.04980111 secs


OP's previous implementation was taking Time difference of 0.2666121 secs for the same







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 14:24









Vivek KalyanaranganVivek Kalyanarangan

4,9411827




4,9411827












  • How would I get the slope for Var2?
    – Mario del Pino
    Nov 19 '18 at 19:20


















  • How would I get the slope for Var2?
    – Mario del Pino
    Nov 19 '18 at 19:20
















How would I get the slope for Var2?
– Mario del Pino
Nov 19 '18 at 19:20




How would I get the slope for Var2?
– Mario del Pino
Nov 19 '18 at 19:20


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375869%2fr-create-mutate-a-new-column-as-a-function-of-past-observations%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]