1

I´m very new to R and am working with EEG´s derived from sleeping patients. Now I have a table similar to this:

 ID   C3-M2  C4-M1  Disorder
 12     347    325   Control
 13     397    346   Bipolar
 14     368    363   Control
 15     370    379   Control
 16     368    310   Bipolar

Because I needed the mean value of C3-M2 and C4-M1 I created a vector:

fast_spin_numb <- c((`C3-M2`+ `C4-M1`)/2)

fast_spin_numb therefore should contain 5 values.

Now I would like to create two separate vectors of which fast_spin_bipo should contain only the means of the bipolar patients and fast_spin_cont only the means of the controls.

I have tried it with

split(fast_spin_numb, disorder == "control", drop = FALSE) 

but I couldn't find a solution how to put the numbers automatically into new vectors.

As well I have tried this:

tapply(fast_spin_numb, disorder, shapiro.test)

which allows me at least to run some tests. But it doesn't help me with creating a qqplot (only for controls) for example.

Thank you very very much already!!

Parfait
  • 104,375
  • 17
  • 94
  • 125
ElenaNick
  • 11
  • 1

1 Answers1

1

Consider by, the object-oriented wrapper to tapply which can subset a dataframe by one or more factor(s) to return a list of output from a function. Below returns a named list of vectors.

Data

txt = ' ID   "C3-M2"  "C4-M1"  Disorder
12     347    325   Control
13     397    346   Bipolar
14     368    363   Control
15     370    379   Control
16     368    310   Bipolar'

df <- read.table(text=txt, header=TRUE)

Code

mean_vectors <- by(df, df$Disorder, FUN=function(i) (i$`C3.M2`+ i$`C4.M1`)/2)

mean_vectors$Control
# [1] 336.0 365.5 374.5
mean_vectors$Bipolar
# [1] 371.5 339.0
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • This adds to my recent, continued campaign to promote the underused `by` in R: [1](https://stackoverflow.com/a/47124608/1422451), [2](https://stackoverflow.com/a/47128403/1422451), [3](https://stackoverflow.com/a/47027343/1422451), [4](https://stackoverflow.com/a/46836601/1422451), [5](https://stackoverflow.com/a/46839140/1422451) – Parfait Nov 07 '17 at 17:21