0
Anybody fix the code?
input <- readLines('stdin') x <- as.integer(input[1]) data <- data.frame( "id" = c(1:10), "grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99) ) student <- 0 for ( i in data$grade){ while( i > x ){ student <- i + student i <- i + 1 print(length(student)) } }
17 Answers
+ 9
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
student <- 0
for ( i in data$grade){
if ( i > x ){
student <- 1 + student
}
}
print(student)
+ 3
#try this
for ( i in data$grade){
if ( i > x ){
student <- 1 + student
}
}
print(student)
+ 2
I've just filtered the data and used length:
print(length(data$grade[data$grade>x]))
+ 2
#it is easier using subset and length function
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
student <- subset(data, data$grade > x)
print(length(student$grade))
+ 1
You are using student as an integer. student needs to be a vector.
+ 1
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
i <- 1
student <- c()
#try this
for ( i in data$grade){
if ( i > x ){
student <- +i
}
print((student))
}
Now is right?
0
You tells like this type Student<- c()
0
This is a question
You have a dataframe that includes grades of students.
Your program needs to take a number as input, and output the number of students who have a grade greater than the given input.
For example, for the input 89, the output should be:
[1] 3
as 3 students have grades greater than 89.
0
You can either count the number of students whose grades meet the condition by adding up an integer or collect them and print the length of the resulting vector. Anyways, don't print inside the loop, but rather only once after the loop.
Another approach on the task is subsetting and indexing (no loop). Maybe you would like to check this approach?
https://www.sololearn.com/Discuss/2883382/?ref=app
0
No, its not correct
0
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
student <- 0
for ( i in data$grade){
if ( i > x ){
student <- 1 + student
}
}
print(student)
0
Please don't post 'answer-only' comments with ready-made code and without any explanation.
0
lra <- subset(data, grade>x)
print(length(lra$grade))
0
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
print(nrow(data[data$grade>x,]))
0
i done like this
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
pass<-factor(c(data$grade[data$grade>x]))
print(length(pass))
0
#try this
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
bigger <- data$grade[x<data$grade]
print(length(bigger))
- 1
input <- readLines('stdin')
x <- as.integer(input[1])
data <- data.frame(
"id" = c(1:10),
"grade" = c(75, 26, 54, 90, 86, 93, 48, 71, 66, 99)
)
student <- 0
for ( i in data$grade){
if ( i > x ){
student <- 1 + student
}
}
print(student)