1
$\begingroup$

I have a dataset for 100 households and 10 years.

enter image description here

I created a new variable called x1hat conditional on the household identifier. Then, I assigned the same value as under variable x1 to all households from 1 to 60. All other households should have a x1hat which equals x1 + 100.

I've tried the following code in r:

#create new variable and add it to dataset
mydataset <- mydataset %>% mutate( x1hat = case_when(
household <= '60' ~ x1,
household > '60' & household <= '100' ~ sum(x1 + 100)))

Unfortunately, this generates a new variable called x1hat (which is good) but it is not able to add 100 to the households above 60. How can I do this?

Many thanks in advance for your support.

$\endgroup$
2
  • 2
    $\begingroup$ Look up ifelse. $\endgroup$ Commented Oct 14, 2022 at 16:44
  • $\begingroup$ x1hat <- x1 + 100 * (household > 60) $\endgroup$ Commented Oct 15, 2022 at 0:34

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.