我刚刚开始使用R程序,并且在弄清楚我的程序出了什么问题时遇到了一些麻烦。我在我的R程序中输入了一个文件,当我试图询问'玉米'和'棉花'的平均值和标准偏差时,我不断收到错误'对象'玉米'未找到'和'对象'棉花'未找到'。以下是我的程序的开头和我导入的.txt文件:
rm(list=ls(all=T))
# data
detach(data)
data<-read.table("D:/stalkeyedflies.txt", header=T)
attach(data)
data
names(data)
summary(data)
# mean and standard deviation of corn
mean(corn)
sd(corn)
# mean and standard deviation of cotton
mean(cotton)
sd(cotton)
这就是.txt文件的样子:
Treatment eye.span
corn 2.15
corn 2.14
corn 2.13
cotton 2.12
cotton 2.07
cotton 2.01
非常感谢你提前!!
答案 0 :(得分:4)
您应该按列进行子集化。例如:
mean(subset(dat,Treatment=='corn')$eye)
[1] 2.14
> mean(subset(dat,Treatment=='cotton')$eye)
[1] 2.066667
或者更好的是,您应该使用tapply
来应用治疗组的平均值:
tapply(dat$eye.span,dat$Treatment,mean)
corn cotton
2.140000 2.066667
这里的数据是:
dat <- read.table(text='Treatment eye.span
corn 2.15
corn 2.14
corn 2.13
cotton 2.12
cotton 2.07
cotton 2.01',header=TRUE)
答案 1 :(得分:0)
data<-read.table("stalkeyedflies.txt", header=T)
data
corn<-data[data$Treatment=="corn",]
# get the subset of corn
mean(corn[,"eye.span"])
# calculate the mean of corn
cotton<-data[data$Treatment=="cotton",]
# get the subset of cotton
sd(cotton[,"eye.span"])
# calculate the sd of cotton
&#13;