[R] Cannot reproduce tutorial results

Jason Hernandez j@@on@hern@ndez74 @end|ng |rom y@hoo@com
Fri Feb 22 22:10:18 CET 2019


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 here 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 <- NULLfor(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?

| 
| 
| 
|  |  |

 |

 |
| 
|  | 
Introduction to Data Science with R - Data Analysis Part 1

Part 1 in a in-depth hands-on tutorial introducing the viewer to Data Science with R programming. The video prov...
 |

 |

 |




	[[alternative HTML version deleted]]



More information about the R-help mailing list