在r中如何通过引用员工的姓名选择数据行:ALEX,BRAD或带变量的JOHN?
从下面的代码我得到:
missing value where TRUE/FALSE needed.
emp1["ALEX","mon"]
[1] NA
csv示例 -
* X.name mon tue wed thu fri
1 ALEX 98 95 73 88 18
2 BRAD 66 25 72 8 32
3 JOHN 22 41 78 43 36
emp1 <- read.csv("C:/Database/data/emp1.csv",as.is=TRUE)
emp2 <- read.csv("C:/Database/data/emp2.csv",as.is=TRUE)
employeename<-"ALEX"
if (emp1[employeename,"mon"] > emp2["2","mon"] & emp1["2","mon"]> emp2["3","mon"]) result<- "SUCCESS" else
result<-"fail"
print (result)
答案 0 :(得分:2)
除了指定列之外,您还可以更改rownames
并删除第一列,这样您就可以像之前调用的那样调用行和列。
> rownames(emp1) <- emp1$X.name
> emp1 <- emp1[,-1]
> emp1["ALEX", "mon"]
## [1] 98
答案 1 :(得分:1)
您必须指定列,在本例中为X.name
:
emp1[emp1$X.name == "ALEX", "mon"]
或
employeename <- "ALEX"
emp1[emp1$X.name == employeename, "mon"]