[英]importing file into R program?
我剛剛開始使用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
非常感謝你!
您應該按列進行子集化。 例如:
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)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.