Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subsetting a dataframe based on values in another dataframe

Tags:

r

subset

sorry an absolute beginner so have some very basic questions!

I have a very large data set that lists individual transactions by a household. Example is below.

#   hh_id trans_type transaction_value
# 1   hh1       food                 4
# 2   hh1      water                 5
# 3   hh1  transport                 4
# 4   hh2      water                 3
# 5   hh3  transport                 1
# 6   hh3       food                10
# 7   hh4       food                 5
# 8   hh4  transport                15
# 9   hh4      water                10

I want to to create a new data frame that has all transactions listed for ONLY the households that have transactions in the "water" category. (Eg, I would want a df without hh3 above because they have not had any expenses in "water")

as a first step, I have a data frame with one column (hh_ids) that only has the household IDs of the ones that I want. How do I then subset my larger dataframe to remove all rows of transactions that are not from a household that have expenses in the "water" category?

Data

## data from @gung
d <- read.table(text="hh_id   trans_type  transaction_value
hh1 food    4
hh1 water   5
hh1 transport   4
hh2 water   3
hh3 transport   1
hh3 food    10
hh4 food    5
hh4 transport   15
hh4 water   10", header=T)
like image 624
Ashley Thomas Avatar asked May 17 '26 22:05

Ashley Thomas


1 Answers

d <- read.table(text="hh_id   trans_type  transaction_value
hh1 food    4
hh1 water   5
hh1 transport   4
hh2 water   3
hh3 transport   1
hh3 food    10
hh4 food    5
hh4 transport   15
hh4 water   10", header=T)

dw <- as.character(with(d, hh_id[trans_type=="water"])) 
ds <- d[which(d$hh_id%in%dw),]
ds
#   hh_id trans_type transaction_value
# 1   hh1       food                 4
# 2   hh1      water                 5
# 3   hh1  transport                 4
# 4   hh2      water                 3
# 7   hh4       food                 5
# 8   hh4  transport                15
# 9   hh4      water                10
like image 175
gung - Reinstate Monica Avatar answered May 19 '26 13:05

gung - Reinstate Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!