Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

R statistics, panel data and NAs: replacing NA value in vector with a specific row in another vector using panel data

$
0
0

My apologies for a poorly formulated question. I am new to R and to programming and for posting questions.

I am working with panel data. I have two context varying variables: cat (category that ranges from 1 to 4, where an individual have gambled in 3 out of 4 possible places) and d.stake = amount of money staked in a given category. Cat and d.stake are nested within the individual (id) (context independent variable).

I wish to make a difference score between the different categories amount staked in different categories.

I have created four variables. Two of them lag is a lag variable (ldstake and ldstake2) and two variables with difference scores (diff1 = stake - ldstake; diff2 stake - ldstake2), using the code

df.3$ldstake <- c(NA, df.3$d.stake[-nrow(df.3)])
df.3$ldstake[which(!duplicated(df.3$id))] <- NA


df.3$ldstake2 <- c(NA, df.3$ldstake[-nrow(df.3)])
df.3$ldstake2[which(!duplicated(df.3$id))] <- NA


df.3 <- df.3 %>%
 mutate(diff1 = d.stake - ldstake,
     diff2 = d.stake - ldstake2)

This give me the following dataframe:

id cat d.stake ldstake ldstake2 diff1 diff2
1   1    50      NA       NA     NA     NA
1   2    60      50       NA     10     NA
1   3    55      60       50     -5     5
2   1    34      NA       NA     NA     NA
2   2    74      34       NA     40     NA
2   4    12      74       34     -62    22

However, I wish to replace the first row of diff1 (the NA) for each individual with the third row of diff2 from each individual (See example below).

id cat d.stake ldstake ldstake2 diff1 diff2
1   1    50      NA       NA     !5!    NA
1   2    60      50       NA     10     NA
1   3    55      60       50     -5     !5!
2   1    34      NA       NA    *22*    NA
2   2    74      34       NA     40     NA
2   4    12      74       34    -62    *22*

Is this possible? I would be grateful to receive a script where I can replace the first NA value with the value of diff2 and last value for the individual (third or last observation). Furthermore, if there is a script that would do this automatically (that is create the difference score between cat2-1 cat3-2 and cat3-1) I would be grateful to receive any help.

All the best, Tony


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>