0
votes

Firstly I need to explain that I've had MINIMAL training on R and have 0 knowledge of coding languages or programmes like R so please excuse me if I ask silly questions or don't understand something basic.

Also, I have tried to look at past topics/answers on this but I'm having a hard time relating the answers to my data so I apologise if this question has already been answered.

Basically I have a data set and I'm trying to find the mean of two variables (Peak flow before a walk in the cold, and peak flow after a walk in the cold) in this set. This is the entire code I've used so far:

 drugs <- read.table(file = "C:\\Users\\Becky\\My Documents\\Asthmadata.txt", header = TRUE)

 drugs
 str(drugs)

 mean.Asthmadata <- tapply (Asthmadata$trial1, list(Asthmadata$PEFR1), mean)
 mean.Asthmadata

It works fine until the mean.Asthmadata. The data comes up in R just fine with the other codes but when I get to the mean and do the mean.Asthmadata [...] code, I keep getting the same error: "object 'mean.Asthmadata' not found"

My friend used the same code I did and it worked for him so I'm confused. Am I doing something wrong?

Thanks

EDIT: @BenBolker

This is my data set

trial1  PEFR1   trial2  PEFR2
Before  310 After   299
Before  242 After   201
Before  340 After   232
Before  388 After   312
Before  294 After   221
Before  251 After   256
Before  391 After   327
Before  401 After   331
Before  287 After   231

And here's all the code I've used:

drugs <- read.table(file = "C:\\Users\\Becky\\My Documents\\Asthmadata.txt", header = TRUE)

drugs
str(drugs)

mean.drugs <- tapply (drugs$trial1, list(drugs$PEFR1), mean)
mean.drugs

The R version I have has two versions: i386 3.1.3, and x64 3.1.3 – I've tried both but neither seem to do what I want. I'm also using Windows 7 Home Premium 64bit. Hope I've included everything you need and I apologise if my formatting is off – I can't quite figure out how to format properly on here yet.

And the error I'm getting NOW is: “Error in split.default(X, group) : first argument must be a vector” when running the code Roland kindly provided. So I'm getting a different error each time I try it – it must be something I'm doing wrong.

Hope I've formatted that all correctly and included everything you need. Thanks :)

1
Did you mean to do mean.Asthmadata <- tapply(drugs$trial1, list(drugs$PEFR1), mean)? You didn't call your data.frame Asthmadata. R doesn't care that that was the file's name. - Roland
I think so? Edit: Yes, I did mean to do what you suggested, however I DID originally do this and it didn't work so I tried changing things. Even when I used drugs$trial1 in the code instead of Asthmadata$trial1 it didn't work either. Said same error except "trial1 not found" instead of "Asthmadata not found". So I'm confused. - Becky Edwards
@Roland I just went back to my data and used the code you gave and now I'm getting this: > mean.Asthmadata <- tapply(drugs$trial1, list(drugs$PEFR1), mean) Warning messages: 1: In mean.default(X[[1L]], ...) : argument is not numeric or logical: returning NA 2: In mean.default(X[[2L]], ...) : argument is not numeric or logical: returning NA 3: In mean.default(X[[3L]], ...) : argument is not numeric or logical: returning NA etc and it's repeating this a further 6 times. (Sorry, not sure how to format it correctly) - Becky Edwards
We really need a reproducible example in order to be able to help. Please review the contents of that link ... - Ben Bolker
@Ben Hi, I've put the info you need in my original comment as it was too long for here and I couldn't figure out how to format it correctly, so please see the edit - thanks - Becky Edwards

1 Answers

1
votes
drugs <- read.table(header=TRUE,text="
trial1  PEFR1   trial2  PEFR2
Before  310 After   299
Before  242 After   201
Before  340 After   232
Before  388 After   312
Before  294 After   221
Before  251 After   256
Before  391 After   327
Before  401 After   331
Before  287 After   231")

In the current format you can calculate the mean before and after just by doing

mean(drugs$PEFR1)

and

mean(drugs$PEFR2)

What you may have had in mind was this shape:

drugs2 <- with(drugs,
                data.frame(trial=c(as.character(trial1),
                                   as.character(trial2)),
                           PEFR=c(PEFR1,PEFR2)))
  • I used with() for convenience -- it's a way to temporarily attach a data frame so you can refer directly to the variables therein.)
  • There's a bit of a pitfall in combining trial1 and trial2, as they get coerced to their numeric codes, which are all 1s in both cases, unless you use as.character() ...
  • you had the order of the variable to aggregate and the variable to group by backwards (you want to aggregate PEFR by trial, not the other way around)
mean.drugs <- with(drugs2,
                   tapply (PEFR, list(trial), mean))
##    After   Before 
## 267.7778 322.6667