1

私は R プログラムを使い始めたばかりで、自分のプログラムの何が問題なのかを理解するのに苦労していました。ファイルを R プログラムにインポートしましたが、'corn' と 'cotton' の平均と標準偏差を求めようとすると、'object 'corn' not found' と 'object 'cotton' not found' というエラーが表示され続けます。以下は、私のプログラムの冒頭と、インポートした .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

よろしくお願いします!

4

2 に答える 2

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)
于 2013-10-30T03:02:50.113 に答える
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

于 2015-05-18T12:44:01.497 に答える