# Median in R per participant for a specific variable

I am currently working on the data from my research. Before I can run the analysis I need to find the median for a specific variable per participant.
My data set consists of all the trials per participant and per trial there is data of that specific variable.

For example:

subjectnr trial variable X
1 1 10
1 2 8
1 3 9.5
1 4 7.5
2 1 9
2 2 12
2 3 10.5
2 4 8

How can I calculate the median for each participant?

Let's say your dta are in a data frame named DF and the column names are subjectnr, trial, and variable_X. To get the mean of variable_X for each subjectnr, you can do the following:

``````library(dplyr)
SubjAvg <- DF |> group_by(subjectnr) |> summarize(Avg = mean(variable_X))
``````

Note that the code requires that you have the dplyr package installed.

I see just now that I made a typo: i meant median in stead of mean. That is why I typed the following in R

SubjAvg <- data_voor_these_integration |> group_by(SubjNr) |> summarize(Avg = median(RT))

And this is the error that I got:

There were 50 or more warnings (use warnings() to see the first 50)

How can I fix this?

``````warnings()
``````

so you can tell us what that actual warnings are.

Warning messages:
1: In mean.default(sort(x, partial = half + 0L:1L)[half + ... :
argument is not numeric or logical: returning NA
2: In mean.default(sort(x, partial = half + 0L:1L)[half + ... :
argument is not numeric or logical: returning NA
3: In mean.default(sort(x, partial = half + 0L:1L)[half + ... :
argument is not numeric or logical: returning NA

This is what I get

It seems your RT column is not numeric. Please post the output of

``````dput(head(data_voor_these_integration))
``````

Place a line with three back ticks just before and after the pasted output, like this
```
pasted output of dput() goes here
```

structure(list(SubjNr = c(10, 10, 10, 10, 10, 10), SubAge = c(29,
29, 29, 29, 29, 29), SubGender = c(2, 2, 2, 2, 2, 2), RefrRate = c(60,
60, 60, 60, 60, 60), StimSet = c(1, 1, 1, 1, 1, 1), Trialnr = c(1,
2, 3, 4, 5, 6), PrimeDifSame = c(1, 0, 0, 1, 0, 1), TargetDifSame = c(1,
1, 0, 1, 1, 0), CapPrime = c(0, 1, 1, 0, 0, 0), Congr = c(1,
0, 1, 1, 0, 0), PrimeLeft = c(71, 100, 103, 69, 69, 65), PrimeRight = c(103,
71, 68, 101, 97, 97), TargetLeft = c(8, 8, 8, 2, 2, 4), TargetRight = c(8,
8, 6, 2, 2, 6), RT = c("0.6559753", "0.406834", "0.4462582",
"0.6019322", "0.480397", "0.4016302"), corrResp = c(1, 1, 0,
1, 1, 0), SubjResp = c(68, 68, 68, 68, 68, 76), Acc = c(1, 1,
0, 1, 1, 1), FixDur = c("0.299999500000013", "0.300012899999274",
"0.300021699999888", "0.300019499999507", "0.300006600000415",
"0.0666160000000673", "0.0666546000002199", "0.0666656000003059",
"0.0666679000005388"), PrimeDur = c("0.0333369999998467", "0.0333193999995274",
"0.0333685999994486", "0.0333405999999741", "0.0333238999992318",
"0.0666780999999901", "0.066660899999988", "0.0666694000001371",
"0.0666556000005585"), Width = c(60, 60, 60, 60, 60, 60), Masked = c(1,
1, 1, 1, 1, 1)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))

The variable "RT" is numeric, but when I write it down there is no option tab/bar where I can click on. When I wrote median or subjnr I got a list with options to choose from and click on. Does the error have something to do with that?

If you see quote marks `"` you are not seeing a numeric, it could be a number represented as a character but not something R knows how to compute on.

``````SubjAvg <- data_voor_these_integration |>
group_by(SubjNr) |>
summarize(Avg = median(as.numeric(RT)))
``````

I have changed the code to
SubjAvg <- data_voor_these_integration |>
group_by(SubjNr) |>
summarize(Avg = median(as.numeric(RT)))

But I got no output/results. I just got a new blue " > "

The results have been put into SubjAvg

You can type SubjAvg into the console to see what it is

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.