如何在R中基于true或false创建直方图?

时间:2013-11-10 05:24:14

标签: r csv import-from-csv

我想要做的是在 R 中创建两个直方图,这取决于海洋世界的员工是否协商加薪,如果他们没有协商加薪,则为1。有人可以告诉我哪里出错了。任何帮助表示赞赏。

这是我正在使用的文本文件的示例。

emp   received   negotiated   gender   year
#325  12.5         TRUE         F      2013
#318  5.2          FALSE        F      2013
#217  9.8          FALSE        M      2013
#223  6.8          TRUE         M      2013
#218  2.1          TRUE         F      2006
#601  13.9         FALSE        M      2006
#225  7.8          TRUE         M      2006
#281  8.5          FALSE        F      2006

这是我到目前为止的代码:

d<-read.csv("employees.txt", header=TRUE, sep="\t")
str(d)

f1 <- mean(d$received)
f2 <- median(d$received)
f3 <- sd(d$recieved)



d$gender <- factor(d$gender, labels=c(1, 2))
pairs(d)

plot(d$received ~ d$gender)
plot(d$received ~ d$year, xlab="year", ylab="recieved")
m <- lm(d$received~d$year)
print(m)
print(f1)
print(f2)
print(f3)
abline(m)
abline(mean(d$received), 0, lty=2)


hist(d$received[d$gender ==1],breaks = 50)
dev.new()
hist(d$received[d$gender ==2],breaks = 50)
dev.new()
#hist(d$year, breaks = 50)
#dev.new()
plot(d$gender, d$received)

1 个答案:

答案 0 :(得分:2)

数据中的#符号对我造成了问题......

使用#符号...

d1 <- read.table(text = "
emp   received   negotiated   gender   year
#325  12.5         TRUE         F      2013
#318  5.2          FALSE        F      2013
#217  9.8          FALSE        M      2013
#223  6.8          TRUE         M      2013
#218  2.1          TRUE         F      2006
#601  13.9         FALSE        M      2006
#225  7.8          TRUE         M      2006
#281  8.5          FALSE        F      2006", 
    header = TRUE)

我们得到一个空数据框......

str(d1)
'data.frame':   0 obs. of  5 variables:
 $ emp       : logi 
 $ received  : logi 
 $ negotiated: logi 
 $ gender    : logi 
 $ year      : logi 

但是没有#我们得到......

d2 <- read.table(text = "
emp   received   negotiated   gender   year
325  12.5         TRUE         F      2013
318  5.2          FALSE        F      2013
217  9.8          FALSE        M      2013
223  6.8          TRUE         M      2013
218  2.1          TRUE         F      2006
601  13.9         FALSE        M      2006
225  7.8          TRUE         M      2006
281  8.5          FALSE        F      2006", 
    header = TRUE)

......数据符合预期:

str(d2)
'data.frame':   8 obs. of  5 variables:
 $ emp       : int  325 318 217 223 218 601 225 281
 $ received  : num  12.5 5.2 9.8 6.8 2.1 13.9 7.8 8.5
 $ negotiated: logi  TRUE FALSE FALSE TRUE TRUE FALSE ...
 $ gender    : Factor w/ 2 levels "F","M": 1 1 2 2 1 2 2 1
 $ year      : int  2013 2013 2013 2013 2006 2006 2006 2006

关于如何创建关于员工收到的加薪幅度的直方图的问题,基于是否要求加薪

hist(d$received[d$negotiated == TRUE])
hist(d$received[d$negotiated == FALSE])