[R] Second attempt: Cannot reproduce tutorial results

Jason Hernandez j@@on@hern@ndez74 @end|ng |rom y@hoo@com
Mon Feb 25 20:37:43 CET 2019


Okay, I switched to plain text. Let's see if people can see this better.
I have come back to trying to learn R after a long time away, and have begun with the YouTube tutorial videos by David Langer, as seen on "Introduction to Data Science with R - Data Analysis Part 1." I am using R Studio with R version 3.4.4 (2018-03-15) -- "Someone to Lean On"

Around 1:16:00 in the video Langer creates a new variable using an else if loop:

extractTitle <- function(name) {
     name <- as.character(name)
     if(length(grep("Miss.", name)) > 0) {
       return("Miss.")
     } else if(length(grep("Master.", name)) > 0) {
       return("Master.")
     } else if(length(grep("Mrs.", name)) > 0) {
       return("Mrs.")
     } else if(length(grep("Mr.", name)) > 0) {
       return("Mr.")
     } else { return("Other.") }}

titles <- NULL
for(i in 1:nrow(data.combined)) {
     titles <- c(title, extractTitle(data.combined[i, "name"])) }

data.combined$title <- as.factor(titles)

There are two problems I see in my attempt to replicate this. First, the data.combined set contains 1309 names, but when I try to create the variable "titles" using this code, it creates a list of 2. When I use View(titles), what comes up is the first item on the list is 

function (main = NULL, sub = NULL, xlab = NULL, ylab = NULL, line = NA, outer = FALSE, ...)

and the second item in the list is just the title "Master."

The second problem is that after I enter data.combined$title <- as.factor(titles) I get the error message

Error in sort.list(y) : 'x' must be atomic for 'sort.list' Have you called 'sort' on a list?

If I try changing the as.factor to as.vector, I get 

Error in `$<-.data.frame`(`*tmp*`, title, value = list(function (main = NULL, : replacement has 2 rows, data has 1309

I have checked and rechecked my code, and it is identical to Langer's. What is wrong here?



More information about the R-help mailing list