0
$\begingroup$

I'm still somewhat new to R, so please bear with me. I'm trying to summarize the results of the variable riskT0. However, because I have some missing values in the variable riskT2 I would like to only include participant values if they have also a value for the riskT2 variable.

reproducible example:

ID <- c(1:300) #participants
group <- c(1:2) #there are two groups
riskT0 <- c(1:2) #value of 1st measurement
riskT2 <- c(1:3) #value of 2nd measurement 
df<- data.frame(ID,group,riskT0,riskT2) #create df
df["riskT2"][df["riskT2"] == 3] <- "NA" #create NAs (I'm sure there are better ways)

What is the simplest way to summarize the riskT0 variable if a riskT2 value is available? Also how to do the same but then do it separately for group 1 and 2?

The way I was trying to get the results so far was the following:

"count(df$riskT0[df$riskT2 !=NA])

which doesn't work. Also I wouldn't know how to filter for group.

Thank you in advance.

$\endgroup$

2 Answers 2

0
$\begingroup$

Don't set "NA" as a string. Set it as its own type NA:

df["riskT2"][df["riskT2"] == 3] <- NA

This does not change the type of the other values: as you have it, riskT2 changes type from integer to character after assignment of "NA".

Then, to address your question, you need to use is.na(x) to check for NA values in a variable x.

For a count of all riskT0 values conditioned on the riskT2 being NA you would write:

table(df$riskT0[!is.na(df$riskT2)])

$\endgroup$
2
  • $\begingroup$ This worked flawlessly, thank you! Would it be possible to also add another condition in this code like 'group ==1' to show the output only of group 1 or 2? $\endgroup$
    – Blubbb
    Sep 27, 2022 at 11:10
  • $\begingroup$ You can use a logical vector (TRUE/FALSE) to select the elements of a vector you're interested in. Above, we used !is.na(...). To also select group 1, modify that logical vector: !is.na(df$riskT2) & df$group == 1. $\endgroup$
    – fm361
    Sep 27, 2022 at 11:32
2
$\begingroup$

If you want to summarize riskT0 you can do

with(subset(df, df$riskT2 != "NA"), summary(riskT0))

subset creates new dataframe out from df by eliminating all the rows of df where riskT2 takes NA. Passing this temporary dataframe to with hepls at making the variable riskT0 available for summary.

$\endgroup$
4
  • $\begingroup$ Thank you, for some reason it didn't work though. The output returns simply: ``` Min. 1st Qu. Median Mean 3rd Qu. Max. ``` without showing any output. $\endgroup$
    – Blubbb
    Sep 27, 2022 at 11:05
  • $\begingroup$ ops! replace NA by "NA". $\endgroup$
    – utobi
    Sep 27, 2022 at 11:13
  • 1
    $\begingroup$ Thank you it works now. However, because my data is based on count variables it seems like table() is more appropriate for my data. with() returns the following output: Min. 1st Qu. Median Mean 3rd Qu. Max. 1.0 1.0 1.5 1.5 2.0 2.0 $\endgroup$
    – Blubbb
    Sep 27, 2022 at 11:31
  • $\begingroup$ if you want a the frequency distribution, just replace summary by table. In the post you talk about "I'm trying to summarize the results of the variable riskT0.", and summary does that. $\endgroup$
    – utobi
    Sep 27, 2022 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.