Reputation: 49
I have a housing dataset which is grouped by the property code. It indicates who owns a property in any given year. The dataset includes year, property code, and name of owner. It also includes a binary variable called "change" which indicates whether the owner of the property has changed.
I want to loop through each property group to find when there is a change in owner (change=1). When it finds a change in owner, it should create a new dataset where one column has the name of the old owner, and the other column has the name of the new owner.
The aim of doing this is to eventually run an analysis of whether the gender or ethnicity of the owner changes. I am using the packages wru and gender, and was going to compare the old owner with the new one after both had been identified.
I'm very new to R and would love if someone could guide me through this.
property_changes <- dataset[,c(1,2,3,10)]
change_col_names <- c("year","change", "property", "name")
colnames(property_changes) <- change_col_names
groups <- group_by(property_changes, property_changes$property)
Upvotes: 0
Views: 75
Reputation: 3252
Welcome to R. I strongly suggest you look at the the package "dplyr" with the function recode(), instead of looping, we can create a new column with a "yes" or "no" for if the ownership of the property has changed, this can allow you to pull only the rows where the ownership changed by filtering. I created a simple example for explanation.
library(dplyr)
year = seq(2000, 2009, 1)
change = c(0,0,0,0,1,0,1,0,0,0)
owner = (c("bob", "bob", "bob", "bob", "alice", "alice", "lisa", "lisa", "lisa", "lisa"))
prop <- data.frame(year, change, owner)
prop %>%
group_by("owner") %>%
mutate(change_in_ownership=recode(change,
`0`="No",
`1`="yes")) %>%
filter(change_in_ownership == 'yes')
where my output after the filtering is:
year change owner `"owner"` change_in_ownership
<dbl> <dbl> <fct> <chr> <chr>
1 2004 1 alice owner yes
2 2006 1 lisa owner yes
Upvotes: 1