From For-Loop to apply

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



From For-Loop to apply



Edit to Include vectors


BaseSal <- c(22745,23606,24761,25873,27097,28533,30153,31813,
33588,35492,37524,39630,41935,44346,46855,49483,
52260,55222,58211,61194,64441,67900,71493,75299,
79455,83641,88169,92811,97673,102780,108257,114015,
120216,126620,133184,139879,147219,137346);

AnnualInc <- c(678,703,730,762,815,843,876,907,939,981,1045,1081,
1125,1201,1245,1293,1355,1327,1382,1440,1503,1566,
1631,1694,1766,1838,1935,2010,2088,2164,2245,2321,
2397,2480,2560,2648,2731,1);



My following code has Vector BaseSal with the Base Salary for 38 different positions, as well as Vector AnnualInc which is the annual amount the Base Salary increases for each of the 38 positions. This code works perfectly:


for (i in 1:38)
print(BaseSal[i]+(AnnualInc[i] * 0:10));



The reason for the rang 0:10 is 0 is the first year salary, and the annual increment is added each year for 10 years.



And I was actually surprised at how little I had to do to get there. I have read a bit about apply being better for use in R, and I have it partially working in apply. The results I get deliver me the value of if the first years annual increase was applied to the base salary. Here is the code I'm using to get there :


l<-matrix(BaseSal,38,11,FALSE);
apply(l,2,function(z) z+(AnnualInc));



Can anyone help me figure out how to iterate through each years increment using a matrix and apply, including leaving the first year as just the base salary?





You need to edit with enough data to make this question reproducible.
– alistaire
Feb 17 '17 at 17:51





@alistaire I've added my vectors, is that suitable or is more needed?
– Josh K
Feb 17 '17 at 18:28




3 Answers
3



The following will return the same values you get from your original for loop. As you can see, is quite similar in notation to a for loop in this case.


for


for


lapply(1:38, function(i)
BaseSal[i]+(AnnualInc[i] * 0:10)
)



Note that lapply returns a list, you can use sapply and get a matix back.


lapply


sapply





thank you @juan-bosco this works (both lapply, and sapply) - I'll have to look into those further. I guess I got a little ahead of myself on my tutorials and tried to do more than I'm able.
– Josh K
Feb 17 '17 at 18:40





Don't worry, apply was weird for me (and I bet for is many) the first time I encountered in R. Check the other answers to this question, they will help you further understand how R handles the kind of opperation you are trying to accomplish.
– Juan Bosco
Feb 17 '17 at 18:47



With apply you can do:


apply


set.seed(48)

baseSal <- rnorm(38, 100, 5)
annInc <- rnorm(38, 10, 1)
mydf <- data.frame(baseSal, annInc)

class(mydf)
[1] "data.frame"

head(mydf)
baseSal annInc
1 100.99880 9.895188
2 86.09930 8.874881
3 96.52124 10.909675
4 110.37700 12.670161
5 103.95111 10.248833
6 102.44925 8.058972

res <- apply(mydf, 1, function(x) x[1] + x[2] * 0:10)



Basically merge the two vector to a single object, i.e. a data.frame, then for each row you calculate what you want.
You will get a 10x38 matrix where each column is a 'progression' and each row is a 'year'


data.frame



You can just make a matrix of the appropriate dimensions and use element-wise matrix operations:


m <- l + AnnualInc * (col(l) - 1)

head(m)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
## [1,] 22745 23423 24101 24779 25457 26135 26813 27491 28169 28847 29525
## [2,] 23606 24309 25012 25715 26418 27121 27824 28527 29230 29933 30636
## [3,] 24761 25491 26221 26951 27681 28411 29141 29871 30601 31331 32061
## [4,] 25873 26635 27397 28159 28921 29683 30445 31207 31969 32731 33493
## [5,] 27097 27912 28727 29542 30357 31172 31987 32802 33617 34432 35247
## [6,] 28533 29376 30219 31062 31905 32748 33591 34434 35277 36120 36963





This is sort of what I was looking for, a way to reference the values already within the matrix, thank you.
– Josh K
Feb 17 '17 at 18:47






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered