dataframe - R text manipulation -


i have dataframe below. want take ww1 column , create new column newww1 follows:

my excel formula

=2012&text((left(201438,4)-2012)*53+right(201438,2),"0000") 

where instead of 201438 have value column ww1

the explanation of formula is:

take left 4 characters of ww1 subtract 2012 them multiply answer 53 add answer right 2 characters of ww1 print answer in "0000" format concatenate answer 2012. 

my data

product=c(rep("a",4),rep("b",2)) ww1=c(201438,201440,201444,201446,201411,201412) ww2=ww1-6 diff=rep(6,6) demand=rep(100,6)  df=data.frame(product,ww1,ww2,diff,demand)  df    product    ww1    ww2 diff demand 1       201438 201432    6    100 2       201440 201434    6    100 3       201444 201438    6    100 4       201446 201440    6    100 5       b 201411 201405    6    100 6       b 201412 201406    6    100 

this how data @ end

    product ww1 ww2 diff    demand  newww1 1     201438  201432  6   100 20120144 2     201440  201434  6   100 20120146 3     201444  201438  6   100 20120150 4     201446  201440  6   100 20120152 5   b   201411  201405  6   100 20120117 6   b   201412  201406  6   100 20120118 

df$newww1 = paste0('2012', sprintf('%04d',                      53 * (as.numeric(substr(df$ww1, 1, 4)) - 2012) +                            as.numeric(substr(df$ww1, 5, 6))                          )                   ) 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -