New to R - how to find out count of a variable based on existing condition of previous variable


I am very new to R and am just doing a course learning it. I am working with 3 categorical variables:

  1. adult told they had asthma
  2. adult still has asthma
  3. gender

I am trying to find out of all those that said "Yes" to "told they had asthma", how many of those said "Yes" to "still has asthma". And finally, by gender.

I am really struggling to do even the first part. Is there anyway I can filter all the "Yes" from the first variable and then find all the "Yes" in the second variable?

I tried using the filter function, but for some reason, it comes back to "0 row" as the result.

Any advice and guidance will be greatly appreciated!

Welcome in the group.

I am not quite sure how you handle those variables.
Can you show how you created them? Please let us know.

Apart from that there is the package forcats that is often helpful with categorical data (factors).

The following gives the counts, but does not identify the individual observations:

df <- data.frame(
gender = c("M", "M", "M", "F", "F", "M","F","F","F", "M"),
told = c("Y","N","Y","N","Y","N","Y","N","Y","N"),
has = c("Y", "Y", "Y", "Y", "Y", "N", "N","N","N","N"))

t <- table(df)

1 Like

Thank you both for your messages.

Does this mean I should convert the data into numerical variables first? Would that work better to what I am trying to do?

Also Re: fcas80, what does the following mean?

t <- table(df)

My data frame is called Q1asthma, so I assume I substitute the first line with "Q1asthma <- Q1asthma (" . Is that correct? And am I supposed to substitute "t" with something else? Because I got the error message "could not find function "Q1asthma" (see screenshot).
Screen Shot 2020-08-19 at 3.51.25 PM|690x387

Thank you so much again, I don't really know what I am doing, both your advice is greatly appreciated!!!

table(df) says convert the dataframe to a a table. ftable creates a contingency table for more than two variables.

Your data is already in a dataframe, so you don't need my first line df <- data.frame( ... I am only using it to generate some sample data to use with table and ftable.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.