-1

I am new to R, so please forgive my ignorance before hand.

I have a dataset that looks like this. jetleg.dat being my dataset.

treatment    phaseshift
control       0.53
control       0.36
  light      -0.78
  light      -0.86

I need to get the mean for treatment and phaseshift.

When I do the calculation,

control <- jetlag.dat[jetlag.dat$treatment == 'control',]$percent
light <- jetlag.dat[jetlag.dat$treatment == 'light',]$percent

mean(control)
mean(light)

I get this notification:

Warning message: In mean.default(control) : argument is not numeric or logical: returning NA

I understand that control and light are non numeric, but I thought I accounted for that in calculation. I have done this before like this and it worked. Any help would be appreciated. Thank you.

Sotos
  • 51,121
  • 6
  • 32
  • 66
Sarah
  • 11
  • 4
  • Can you give a reproducibe code? Go through http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Kumar Manglam Feb 23 '17 at 12:55

2 Answers2

0

You are referring to a column named percent, which I can't see. I'll assume it should be $phaseshift.

control <- jetlag.dat[jetlag.dat$treatment == 'control',]$phaseshift
light <- jetlag.dat[jetlag.dat$treatment == 'light',]$phaseshift

mean(control)
mean(light)

If this still gives you the same error, try to wrap it in as.numeric.

mean(as.numeric(control))

PinkFluffyUnicorn
  • 1,260
  • 11
  • 20
0

It seems to be mostly syntax that is your issue here.

First, you call $percent when your variable is names phaseshift in the previous line.

Second, the $percent or $phaseshift needs to be before the brackets

and Finally, there shouldn't be any comma after jetlag.dat$treatment == "control" statement.

Here is a revised version of your code and it works:

df <- data.frame(treatment = c("control", "control", "light", "light"),
             phaseshift = c(0.53, 0.36, -0.78, -0.86))

control <- df$phaseshift[df$treatment == "control"]
light <- df$phaseshift[df$treatment == "light"]

control_mean <- mean(control)
light_mean <- mean(light)

Hope this helps!

tbradley
  • 2,210
  • 11
  • 20