0

I am writing an if else statement that takes an input (tool, in the form of a barcode number) and outputs the tool name, and location.
I'm also writing it so that when a certain Barcode Number(tool) is input, it adds to the running total that I have in column name (TIMES.USED). I am having problems though because I am trying to add a check in/out column(Status), that basically reads that if an items usage value is divisible by 2, the item is checked in. If it is not divisible by 2, the item is checked out. Here is the structure of my data frame and the code that I have written so far:

Data Frame:

Barcode.Number,    Type.Equipment,   Manufacture,   Times.Used,   Status
4041,             Flash Light,      Surefire,      1
4044,              Rope,             Surefire,      2      

Code:

tool = as.integer(readline(prompt="Enter a number: "))
df1 <- df %>% filter(df$Barcode.Number == tool) %>% 
select(Type.Equipment, Manufacture)    
if((df$TIMES.USED %% 2) == 0) {
df$Status <- 'In'
} else {
df$Status <- 'Out'
}

I can get it so that when I input a number like 4041, it can see that the 1 is odd and outputs 'out' into the Status column. The problem is that I'm not sure how to write it so that it outputs different status' into different rows because it just outputs into all of them. Thank you!

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • I am not sure on the strcture of the list of tools but since you prompt the user to select the tool, we can use that number to index the row or column in status you want to change. Something like `df$Status[tool] <- "in or out"` – Chabo Jul 05 '18 at 16:59
  • 1
    You should use the `ifelse` function and not the if then else. `ifelse((df$TIMES.USED %% 2) , 'In' , 'Out')` – Dave2e Jul 05 '18 at 17:04
  • Thank you so much for your help @Dave2e, if you dont mind me asking, how can I move the output from this into a data frame. It outputs perfectly but I want to make it so that it can update the data frame and column 'Status'. Thank you! – Greg Pedersen Jul 05 '18 at 17:13
  • 1
    Just assign it: `df$Status <- ifelse(...Dave's code...)`. Since you seem to be using `dplyr`, you code also do `df = mutate(df, Status = ifelse(TIMES.USED %% 2, 'In', 'Out'))` – Gregor Thomas Jul 05 '18 at 17:14
  • Thank you everyone for all of the help! I am very new to using r so I appreciate your patience. – Greg Pedersen Jul 05 '18 at 17:23

1 Answers1

0

Using match we can find the row at which the barcode requested exists, we assign this index. We then use index to change the correct status value.

tool = as.integer(readline(prompt="Enter a number: "))
index<-match(tool, df$Barcode.Number)

df1 <- df %>% filter(df$Barcode.Number == tool) %>% 
select(Type.Equipment, Manufacture)    
if((df$TIMES.USED %% 2) == 0) {
df$Status[index] <- 'In'
} else {
df$Status[index] <- 'Out'
}

For Example..

Tool<-c(10,21,31,42,51)
Data<-c("a","b","c","d","e")
Status<-c(NA,NA,NA,NA,NA)

df<-data.frame(Tool,Data,Status)

index<-match(10, df$Tool)

df$Status[index] <- 'In'

  Tool Data Status
1   10    a     In
2   21    b   <NA>
3   31    c   <NA>
4   42    d   <NA>
5   51    e   <NA>
Chabo
  • 2,842
  • 3
  • 17
  • 32