0

In r how can I select a data row by referencing the name of the employee : ALEX ,BRAD or JOHN with a variable?

From the code below I get:

missing value where TRUE/FALSE needed.
emp1["ALEX","mon"] 
[1] NA

csv example-

*  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)
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
  • Show us what your data looks like. Here are a few pointers on how to provide a good example to harvest best results: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example You will probably need at least `head(emp1)`. – Roman Luštrik Mar 26 '14 at 06:54
  • the data is under csv example – user3008387 Mar 26 '14 at 12:11

2 Answers2

2

In addition to specifying the column, you can change the rownames and delete the first column, enabling you to call the row and column as you were previously calling it.

> rownames(emp1) <- emp1$X.name
> emp1 <- emp1[,-1]
> emp1["ALEX", "mon"]
## [1] 98
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
1

You have to specify the column, in this case X.name:

emp1[emp1$X.name == "ALEX", "mon"]

or

employeename <- "ALEX"
emp1[emp1$X.name == employeename, "mon"]
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168