+ 3

Titanic Survivors

x <- read.csv('/usercode/files/titanic.csv') y = x["Age">=18,] z = by(y$Pclass,y$Survived,mean) z = c(z) print(z) I cannot figure out what is wrong with my code? And why? It is printing the mean values which are wrong. I am interested in the right answer, but more interested in where have I been wrong.

6th Nov 2021, 9:52 PM
Nehul Patel
Nehul Patel - avatar
4 ответов
+ 6
x <- read.csv('/usercode/files/titanic.csv') y <- x[x["Age"]>=18,] z <- by(y$Pclass,y$Survived,mean) z <- c(z) z ——————————— 👇 x <- read.csv('/usercode/files/titanic.csv') y <- x[x$Age>=18,] c(by(y$Pclass,y$Survived,mean))
6th Nov 2021, 11:05 PM
Solo
Solo - avatar
+ 1
This x <- read.csv('/usercode/files/titanic.csv') y <- x[x$Age>=18,] print(c(by(y$Pclass,y$Survived,mean)))
12th Aug 2022, 4:58 AM
Harmony Ikenna James
Harmony Ikenna James - avatar
0
x <- read.csv('/usercode/files/titanic.csv') y = x["Age">=18,] z = by(y$Pclass,y$Survived,mean) z = c(z) print(z) The code snippet above reads the Titanic data from a CSV file and assigns it to the variable x. Then, it filters the data to include only passengers with an age greater than or equal to 18 and stores it in the variable y. Next, the code uses the by() function to calculate the mean values of the Pclass column grouped by the Survived column in the filtered data y. The result is stored in the variable z. Finally, the code converts the result into a vector using the c() function and prints the values using the print() function. Now, let's address the issue with the code and identify the mistakes. Mistakes in the Code Incorrect Syntax: The code snippet provided is written in HTML format, but it should be written in R programming language. To fix this, we need to change the code language to R. Incorrect Filtering: The filtering condition ["Age">=18,] is not valid in R. To filter the data based on t
31st Jul 2023, 9:00 PM
Super sonic speed
Super sonic speed - avatar
0
Corrected Code x <- read.csv('/usercode/files/titanic.csv') y <- x[x$Age >= 18, ] z <- aggregate(y$Pclass, by = list(y$Survived), FUN = mean) print(z)
31st Jul 2023, 9:01 PM
Super sonic speed
Super sonic speed - avatar