Jason Hernandez
2019-Feb-25 19:37 UTC
[R] Second attempt: Cannot reproduce tutorial results
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?
William Dunlap
2019-Feb-25 20:06 UTC
[R] Second attempt: Cannot reproduce tutorial results
Do you see anything wrong with this line? titles <- c(title, extractTitle(data.combined[i, "name"])) } Hint - plural or singular? Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Feb 25, 2019 at 11:38 AM Jason Hernandez via R-help < r-help at r-project.org> wrote:> 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? > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]