Change variable value based on row index {R} -


main problem

i attempting expand dataset copy rows systematically change 1 variable (year) in each row based the value of value (survey). each survey round took place on multiple years, , data reflect this.

example

data in data.frame, , this:

mydata:       region          year          iv          dv         survey 1    usa.vermont     1999          1.469       6          1 2    usa.vermont     2002          1.698       7          2 3    usa.vermont     2005          1.556       6          3 4    usa.newhamp     1999          0.518       2          1 5    usa.newhamp     2002          0.698       3          2 6    usa.newhamp     2005          0.431       1          3 

i used mydata.2 <- mydata[rep(1:nrow(mydata),each=3),] , got this:

mydata.2:       region          year          iv          dv         survey 1    usa.vermont     1999          1.469       6          1 1.1  usa.vermont     1999          1.469       6          1 1.2  usa.vermont     1999          1.469       6          1 2    usa.vermont     2002          1.698       7          2 2.1  usa.vermont     2002          1.698       7          2 2.2  usa.vermont     2002          1.698       7          2 3    usa.vermont     2005          1.556       6          3 3.1  usa.vermont     2005          1.556       6          3 3.2  usa.vermont     2005          1.556       6          3 4    usa.newhamp     1999          0.518       2          1 4.1  usa.newhamp     1999          0.518       2          1 4.2  usa.newhamp     1999          0.518       2          1 5    usa.newhamp     2002          0.698       3          2 5.1  usa.newhamp     2002          0.698       3          2 5.2  usa.newhamp     2002          0.698       3          2 6    usa.newhamp     2005          0.431       1          3 6.1  usa.newhamp     2005          0.431       1          3 6.2  usa.newhamp     2005          0.431       1          3 

that gets me close, this (notice ascending values of year variable):

mydata.2:       region          year          iv          dv         survey 1    usa.vermont     1999          1.469       6          1 1.1  usa.vermont     2000          1.469       6          1 1.2  usa.vermont     2001          1.469       6          1 2    usa.vermont     2002          1.698       7          2 2.1  usa.vermont     2003          1.698       7          2 2.2  usa.vermont     2004          1.698       7          2 3    usa.vermont     2005          1.556       6          3 3.1  usa.vermont     2006          1.556       6          3 3.2  usa.vermont     2007          1.556       6          3 4    usa.newhamp     1999          0.518       2          1 4.1  usa.newhamp     2000          0.518       2          1 4.2  usa.newhamp     2001          0.518       2          1 5    usa.newhamp     2002          0.698       3          2 5.1  usa.newhamp     2003          0.698       3          2 5.2  usa.newhamp     2004          0.698       3          2 6    usa.newhamp     2005          0.431       1          3 6.1  usa.newhamp     2006          0.431       1          3 6.2  usa.newhamp     2007          0.431       1          3 

how can accomplish this?

if more information needed, happily add it!

thank you!


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -