I have a dataset that looks like this:
you can get sample data using code:
df <- structure(list(Var = c("v1", "v2", "v3", "v4", "v5", "v6", "v7",
"v8", "v9", "v10"), Domain = c("Group1", "Group1", "Group1",
"Group4", "Group4", "Group4", "Group2", "Group2", "Group3", "Group3"
), Groupvar = c("v1", NA, NA, "v5", NA, NA, NA, NA, "v10", NA
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
I would like to build a new variable "Action" to define the action I want to do further using the rule: If there is a !is.na groupvar within same domian, the action=="Merge", if no !is.na groupvar for that domain, then Action=="Append". The dinal data should looks like this:
what should I do? I think I should use lapply( split(df, df$Domain) , ...
, I am new to R and still get confused with list and data.Frame. Not sure what I should do after I split the data by domain. What I want to do is sth like egen. Any guidance on what I should do in order to build variable "Action" will be high appreciated. Thanks.