Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 警告:参数不是数字_R - Fatal编程技术网

R 警告:参数不是数字

R 警告:参数不是数字,r,R,我是新手,请原谅我的无知 我有一个看起来像这样的数据集。jetleg.dat是我的数据集 treatment phaseshift control 0.53 control 0.36 light -0.78 light -0.86 我需要得到治疗和相移的平均值 当我计算的时候 control <- jetlag.dat[jetlag.dat$treatment == 'control',]$percent light <-

我是新手,请原谅我的无知

我有一个看起来像这样的数据集。jetleg.dat是我的数据集

treatment    phaseshift
control       0.53
control       0.36
  light      -0.78
  light      -0.86
我需要得到治疗和相移的平均值

当我计算的时候

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

mean(control)
mean(light)

control您指的是一个名为
percent
的列,我看不到。我假设它应该是
$phaseshift

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

mean(control)
mean(light)

control这里的主要问题似乎是语法

首先,当变量在前一行中为names
phaseshift
时,调用
$percent

其次,
$percent
$phaseshift
需要放在括号之前

最后,在
jetlag.dat$treatment==“control”
语句之后不应该有任何逗号

下面是您的代码的修订版本,它可以正常工作:

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)

df您能给出一个可复制的代码吗?经过