+ 1

[SOLVED] What's wrong in this code?

***Titanic Survivors*** You are working on the Titanic Survivors data set, which includes information on the passengers of the ship. The data is stored in a CSV file, which is already imported in the given code. You want to understand how the class of the ticket impacted the survival rate. For that, you need to find and output the mean class value for the passengers who are adults (Age >= 18), grouped by the Survived column. //////////////////////////////////////////////////// x <- read.csv('/usercode/files/titanic.csv') #your code goes here y <- x[x$Survived == 1, mean(x$Pclass)] z <- x[x$Survived == 0, mean(x$Pclass)] print(mean(y)) print(mean(z)) ////////////////////////////////////// https://code.sololearn.com/cNCjN1gN0NY3/?ref=app https://code.sololearn.com/c5RgUl0RAuwv/?ref=app

30th Aug 2021, 5:49 PM
mesarthim
mesarthim - avatar
19 odpowiedzi
- 1
I solved my problem, finally! :)
30th Aug 2021, 6:27 PM
mesarthim
mesarthim - avatar
+ 12
x <- read.csv('/usercode/files/titanic.csv') #print(x) y <- x[(x$Age >=18),] #print(y) z <- c(by(y$Pclass, y$Survived, mean)) print(z)
15th Oct 2021, 7:54 AM
Abhishek Kumar
Abhishek Kumar - avatar
+ 2
Please put your code in a playground script instead of copying it in the description. Read the task instruction carefully: It's only passengers who were minimum 18 years old. Plus, I think, they intend us to use tapply or something like that.
30th Aug 2021, 5:54 PM
Lisa
Lisa - avatar
+ 2
In version 2 remove the == 1 because we want to see both groups and again: only passengers >= 18
30th Aug 2021, 6:10 PM
Lisa
Lisa - avatar
+ 2
Mohamed Essaied Hamrita Thanks a lot for the comment. I've already solved the problem, as you see before 1 month ago. :) Happy coding!
30th Sep 2021, 4:18 PM
mesarthim
mesarthim - avatar
+ 2
print (c(by(x$Pclass[x$Age >= 18],x$Survived[x$Age >= 18], mean)))
15th Dec 2021, 2:00 PM
Marvin Manaog
Marvin Manaog - avatar
+ 1
print (c(by(x$Pclass[x$Age >= 18],x$Survived[x$Age >= 18], mean)))
24th Sep 2021, 2:39 PM
David García Prados
+ 1
These codes works with me xx <- x[x$Age >= 18,] c(by(xx$Pclass ,xx$Survived, mean))
30th Sep 2021, 3:54 PM
Mohamed Essaied Hamrita
Mohamed Essaied Hamrita - avatar
+ 1
x <- read.csv('/usercode/files/titanic.csv') #print(x) y <- x[(x$Age >=18),] #print(y) z <- c(by(y$Pclass, y$Survived, mean)) print(z)
15th Dec 2021, 2:02 PM
Marvin Manaog
Marvin Manaog - avatar
+ 1
x <- read.csv('/usercode/files/titanic.csv') #print(x) y <- x[(x$Age >=18),] #print(y) z <- c(by(y$Pclass, y$Survived, mean)) print(z)
16th Dec 2021, 9:59 AM
Mirmahmud Ilyosov
Mirmahmud Ilyosov - avatar
+ 1
The given code takes 3 numbers as input. Complete the program to output the maximum of the 3 inputs.
10th Jan 2022, 5:02 PM
stephen mazola
stephen mazola - avatar
+ 1
#print(x) y <- x[(x$Age>=18), ] #print(y) z <- c(by(y$Pclass,y$Survived,mean)) print(z)
9th Apr 2022, 9:11 AM
Farooq Shabbir
30th Aug 2021, 5:59 PM
mesarthim
mesarthim - avatar
0
Lisa look at this please, when I use tapply, I don't understand what's wrong :( https://code.sololearn.com/c5RgUl0RAuwv/?ref=app
30th Aug 2021, 6:06 PM
mesarthim
mesarthim - avatar
0
(The input file doesn't work on playground, I think)
30th Aug 2021, 6:10 PM
Lisa
Lisa - avatar
0
Lisa It works but I couldn't apply the age range, I mean only age >= 18 so the result in the playground a little bit different than it wants I made it buddy, now it works correctly :)
30th Aug 2021, 6:22 PM
mesarthim
mesarthim - avatar
0
Thanks Lisa :)
30th Aug 2021, 6:27 PM
mesarthim
mesarthim - avatar
0
I also have a problem with this code it doesn't take input, but I run it correctly in rstudio x <- read.csv('/usercode/files/titanic.csv') y <- x[x$Age >= 18, ] print(y) z <- c(by(y$Pclass,y$Survived,mean)) print(z)
29th May 2022, 12:45 AM
anahita
0
Here is a more detailed solution x <- read.csv('/usercode/files/titanic.csv') #filtering by age first age <- subset(x, Age >=18) #then calculating mean meanByClass <- by(age$Pclass, age$Survived, mean) res <- c(meanByClass) print(res)
19th Sep 2022, 6:25 AM
Mohammed Shoaib Mohiuddin