编辑代码

student_data <- read.csv("../student_score.csv")
student_data
head(student_data,3)
str(student_data)
dim(student_data)
summary(student_data)
anyNA(student_data)
missing_count <- colSums(is.na(student_data))
print(missing_count)
library(VIM)
aggr(student_data, col = c('navyblue', 'yellow'), numbers = TRUE, sortVars = TRUE, labels = names(student_data), cex.axis = 0.7, gap = 3, ylab = c("Missing data", "Pattern"))
for (col in names(student_data)) {
# 判断该列是否存在缺失值
if (any(is.na(student_data[, col]))) {
# 计算该列的均值(针对数值型列),通过设置na.rm = TRUE忽略缺失值计算均值
mean_value <- mean(student_data[, col], na.rm = TRUE)
# 使用计算出的均值填补该列的缺失值
student_data[, col][is.na(student_data[, col])] <- mean_value
}
}
new_row <- data.frame(
Name = "李文浩",
ID = "022900210121",
design = 85,
computer = 90,
English = 80,
stat = 75,  # 这里你可以自定义具体的成绩数值,我暂设为75
sport = 88  # 同样,自定义成绩数值,暂设为88
)
new_data <- rbind(student_data, new_row)
df_LWH <- new_data
df_LWH
df_LWH$Max_Score <- apply(df_LWH[, c("design", "computer", "English", "stat", "sport")], 1, max)
head(df_LWH)
design_scores <- df_LWH$design
excellent_threshold <- 90
good_threshold <- 80
medium_threshold <- 70
pass_threshold <- 60
grades <- character(length(design_scores))
for (i in 1:length(design_scores)) {
score <- design_scores[i]
if (score >= excellent_threshold) {
grades[i] <- "优秀"
} else if (score >= good_threshold) {
grades[i] <- "良好"
} else if (score >= medium_threshold) {
grades[i] <- "中等"
} else if (score >= pass_threshold) {
grades[i] <- "及格"
} else {
grades[i] <- "不及格"
}
}
for (i in 1:length(grades)) {
cat("第", i, "个学生程序设计基础成绩等级为:", grades[i], "\n")
}
my_student_id <- "022900210121"
my_student_name <- "李文浩"
filename <- paste("C:/Users/Administrator/Desktop/022900210121-李文浩/022900210121/", my_student_id, "-", my_student_name, ".csv", sep = "")
filename <- paste("C:/Users/Administrator/Desktop/022900210121-李文浩/022900210121/", my_student_id, "-", my_student_name, ".csv", sep = "")
write.csv(df_LWH, file = filename, row.names = FALSE)
grades <- data.frame(
Name = c("Alice", "Bob", "Emily", "Liam", "Sophia", "Ava", "bella"),
design = c(69, 89, 100, 80, 70, 78, 81),
computer = c(72, 92, 95, 85, 68, 66, 88),
English = c(93, 85, 63, 89, 79, 75, 69),
state = c(81, 95, 89, 92, 70, 72, 81),
sport = c(95, 80, 85, 91, 89, 90, 95)
)
calculate_grade_sum <- function(name) {
row_num <- match(name, grades$Name)
return(rowSums(grades[row_num, 2:6]))
}
calculate_grade_sum("bella")
match("bella", grades$Name)
rowSums(grades[7, 2:6])
students <- data.frame(
Name = c("Alice", "Bob", "Emily", "Liam", "Sophia", "Ava", "bella"),
design = c(69, 89, 100, 80, 70, 78, 81),
computer = c(72, 92, 95, 85, 68, 66, 88),
English = c(93, 85, 63, 89, 79, 75, 69),
state = c(81, 95, 89, 92, 70, 72, 81),
sport = c(95, 80, 85, 91, 89, 90, 95)
)
students_no_name <- students[, -1]
library(fmsb)
students_radar <- students_no_name
max_scores <- apply(students_no_name, 2, max)
min_scores <- apply(students_no_name, 2, min)
students_radar <- rbind(max_scores, min_scores, students_radar)
radarchart(students_radar, axistype = 1,
pcol = rainbow(7), pfcol = rainbow(7, alpha = 0.5), plwd = 2,
cglcol = "grey", cglty = 1, axislabcol = "grey", caxislabels = seq(0, 100, 20),
cglwd = 0.8)
library(reshape2)
library(ggplot2)
students_long <- melt(students, id.vars = "Name", variable.name = "Course", value.name = "Score")
ggplot(students_long, aes(x = Name, y = Score, fill = Course)) +
geom_bar(stat = "identity", position = "dodge") +
ggtitle("李文浩 score") +
xlab("Students") +
ylab("Scores")