0

Using RStudio After running a my code and inputting an example/test of variables, the variables are not inputted into the right rows in my dataset.

I was given a data set that already had 15 already completed sets of data, I had to write a code to input certain variables. Which is shown below. The inputted data does not show up in the right columns.

enter image description here

read.csv("MontrealDogs.csv", header = T, stringsAsFactors = FALSE)

 
# Create the data frame
MontrealDogs <- "MontrealDogs.csv"(
  DogName = character(),
  LicenceNumber = numeric(),
  Breed = character(),
  Sex = character(),
  OwnerName = character(),
  OwnerPhone = numeric(),
  BitingRecord = logical()
)

# Issue a license
Issue <- function(MontrealDogs, DogName, OwnerName, Breed, Sex, OwnerPhone){
  LicenceNumber <- max(MontrealDogs$LicenceNumber) + 12
  BitingRecord <- FALSE
  MontrealDogs <- rbind(MontrealDogs, 
                         c(DogName, LicenceNumber, Breed, Sex, OwnerName, OwnerPhone, BitingRecord))
  print(paste("The new licence number is", LicenceNumber))
  return(MontrealDogs)
}

# Query
Query <- function(MontrealDogs, LicenceNumber){
  if (LicenceNumber %in% MontrealDogs$LicenceNumber){
    OwnerName <- MontrealDogs$OwnerName[MontrealDogs$LicenceNumber == LicenceNumber]
    OwnerPhone <- MontrealDogs$OwnerPhone[MontrealDogs$LicenceNumber == LicenceNumber]
    print(paste0("The owner's name is ", OwnerName, " and the phone number is ", OwnerPhone))
  } else {
    print("The dog stays at the SPCA until the owner picks it up and gets a licence.")
  }
}

# Delete a licence
Delete <- function(MontrealDogs, LicenceNumber){
  MontrealDogs <- MontrealDogs[MontrealDogs$LicenceNumber != LicenceNumber, ]
  return(MontrealDogs)
}

# Report crime
Report <- function(MontrealDogs, OwnerName, DogName){
  if (OwnerName %in% MontrealDogs$OwnerName & DogName %in% MontrealDogs$DogName){
    BitingRecord <- TRUE
    MontrealDogs$BitingRecord[MontrealDogs$OwnerName == OwnerName] <- BitingRecord
  }
  return(MontrealDogs)
}

# List biters
List <- function(MontrealDogs){
  Biters <- MontrealDogs[MontrealDogs$BitingRecord == TRUE, ]
  print(Biters)
}

# Main program
# Read in the MontrealDogs.csv file
MontrealDogs <- read.csv("MontrealDogs.csv", header = TRUE, stringsAsFactors = FALSE)

# Outermost portion of the main program
quit <- FALSE
while(quit == FALSE){
  command <- readline(prompt = "Please enter a command (Issue, Query, Delete, Report, List, Quit): ")
  if (command == "Issue"){
    DogName <- readline(prompt = "Please enter the dog's name: ")
    OwnerName <- readline(prompt = "Please enter the owner's name: ")
    Breed <- readline(prompt = "Please enter the breed: ")
    Sex <- readline(prompt = "Please enter the sex (F/M): ")
    OwnerPhone <- readline(prompt = "Please enter the owner's phone number: ")
    MontrealDogs <- Issue(MontrealDogs, DogName, OwnerName, Breed, Sex, OwnerPhone)
  } else if (command == "Query"){
    LicenceNumber <- readline(prompt = "Please enter the licence number: ")
    Query(MontrealDogs, LicenceNumber)
  } else if (command == "Delete"){
    LicenceNumber <- readline(prompt = "Please enter the licence number: ")
    MontrealDogs <- Delete(MontrealDogs, LicenceNumber)
  } else if (command == "Report"){
    OwnerName <- readline(prompt = "Please enter the owner's name: ")
    DogName <- readline(prompt = "Please enter the dog's name: ")
    MontrealDogs <- Report(MontrealDogs, OwnerName, DogName)
  } else if (command == "List"){
    List(MontrealDogs)
  } else if (command == "Quit"){
    quit <- TRUE
  }
}

# Write out the dog database as a .csv file
write.csv(MontrealDogs, "MontrealDogs.csv")

enter image description here

As shown in my dataset, I used titles with examples to show what I had inputted but they are clearly not under the right columns and I do not know how to fix this. Any help would be greatly appreciated :)

Thank you!

I cannot really know how to get the correct inputted variables in the correct columns.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • Welcome to StackOverflow. Please make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and provide your data using `dput()` or use a built-in dataset? – jrcalabrese Dec 06 '22 at 21:40

0 Answers0