0

我正在练习 R 编程课程的讲座,在他演示 tapply() 函数的其中一个讲座中,我只是复制粘贴了讲座中教授的内容,但出现语法错误

x <- c(norm(10),runif(10), rnorm(10,1))
f <- g1(3,10)
tapply(x, f, mean)

结果应如下所示

tapply(x, f, mean)
1 2 3
0.1144464 0.5163468 1.2463678

但我得到的是一个错误值

x <- c(norm(10),runif(10), rnorm(10,1))
Error in norm(10) : 'A' must be a numeric matrix
f <- g1(3,10)
Error: could not find function "g1"
tapply(x, f, mean)
Error in tapply(x, f, mean) : object 'f' not found
4

1 回答 1

1

You just have some typos in your code:

x <- c(rnorm(10),runif(10), rnorm(10,1))
f <- gl(3,10)
tapply(x,f,mean)

This will give you the output you want.

Your typos are:

g1 should be gl

and

norm should be rnorm

update

No problem. It might be helpful to learn about apropos in R. It's sort of like a search function for functions/objects. Documentation

If you can't remember the name of a function (let's say rnorm) but you can remember the beginning of it (rnor), you can type

apropos("rnor")

which will return

[1] "rnorm"

Then you can type ?rnorm to access the documentation for rnorm.

于 2014-07-24T19:49:57.343 回答