Create variable or edit an existing variable in panel data

HI! I have a panel data, I a need to edit one of the variables, or create a new one. The data looks like the following;

Id  Year      X1     X2       X3   ...
1   1951  45939  21574 2876  
1   1956  60423  29990 4708 
1   1957  64721  32510 5230 
1   1958  68484  35218 6662 
1   1959  71799  37598 6856 
1   1960  76036  40341 8220
1   1961  79831  43173 9053 
2   1951  45939  21574 2876  
2   1956  60423  29990 4708 
2   1957  64721  32510 5230 
2   1958  68484  35218 6662 
2   1959  71799  37598 6856 
2   1960  76036  40341 8220
2   1961  79831  43173 9053 

I need to change the values of x2 into new ones. I think is quite easy, but I kinda new in r
Thanks!

To create or modify variables, you can use the mutate() function from dplyr package, see this example.

# Sample Data
df <- data.frame(
          Id = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2),
        Year = c(1951, 1956, 1957, 1958, 1959, 1960, 1961, 1951, 1956, 1957,
                 1958, 1959, 1960, 1961),
          X1 = c(45939, 60423, 64721, 68484, 71799, 76036, 79831, 45939, 60423,
                 64721, 68484, 71799, 76036, 79831),
          X2 = c(21574, 29990, 32510, 35218, 37598, 40341, 43173, 21574, 29990,
                 32510, 35218, 37598, 40341, 43173),
          X3 = c(2876, 4708, 5230, 6662, 6856, 8220, 9053, 2876, 4708, 5230,
                 6662, 6856, 8220, 9053)
)

library(dplyr)

df %>% 
    mutate(X2 = X2 * 2,
           X3 = 0,
           new_X = X1 + X2)
#>    Id Year    X1    X2 X3  new_X
#> 1   1 1951 45939 43148  0  89087
#> 2   1 1956 60423 59980  0 120403
#> 3   1 1957 64721 65020  0 129741
#> 4   1 1958 68484 70436  0 138920
#> 5   1 1959 71799 75196  0 146995
#> 6   1 1960 76036 80682  0 156718
#> 7   1 1961 79831 86346  0 166177
#> 8   2 1951 45939 43148  0  89087
#> 9   2 1956 60423 59980  0 120403
#> 10  2 1957 64721 65020  0 129741
#> 11  2 1958 68484 70436  0 138920
#> 12  2 1959 71799 75196  0 146995
#> 13  2 1960 76036 80682  0 156718
#> 14  2 1961 79831 86346  0 166177

Created on 2019-07-27 by the reprex package (v0.3.0.9000)

Thank you very much for the reply. My data set is a data panel similar to the one I copied there, but much more complex. The X3 would be the price of corn for the years 2005-2015. And the ID are municipalities. Obviously the price of corn varies from year to year, not from municipality to municipality. But in my data set there are different numbers, due to a problem of transferring data from wide to long format. I tried the indicated commands but I can't. Basically I have to tell the program to compute the same corn price for the same year. I have over 560 ID´s

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Id  Year           X1        X2           X3          X4  (recoded variable)
1   1951     45939     21574       2876      2876
1   1956  60423      29990         4708      4708
1   1957  64721      32510         5230      5230


2   1951  45939      21574           2.872     2872
2   1956  60423     29990           47.08      4702
2   1957  64721     32510           52.30      5230

3   1951  45939      21574           2.872     2872
3   1956  60423     29990           47.08      4702
3   1957  64721     32510           52.30      5230

The data looks like this example. it is a panel data, the cross sectional is the ID (country departments, almost 450 ID´s) and the time series variable is the years. X3 variable has to be the same for every ID. take a look to the example, they look similar, but they are not due to some Long/wide panel data transfer. The question is how to recode this variable, so it has the same numbers for all ID´s. The variable has no change within ID, but they change from year to year. Basically, it has to have the same number for each year. X4 is the how I would like it to look.

Sorry but your sample data makes no sense to me since X4 values are not the same for all IDs and you are not explaining the logic to select the correct values to apply for all the other IDs, if I was you I would try to go some steps back and try to correctly import the data from the beginning.
Also please read the link I gave you before and try to make a proper reproducible example, by posting your data in this format, you are making unnecessarily hard to help you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.