英文:
Modifying Elements in a Matrix - R
问题
以下是您代码的部分翻译:
#以下是代码中的主要问题
university_matrix[usa_universities, "world_rank"] <- 2
请注意,您的代码的问题是最后一行没有按预期运行。您期望将美国大学(Harvard、Stanford、MIT和Columbia)的排名更改为2,但实际上,数据看起来没有按预期工作。
如果您有关于为什么代码没有按预期工作的问题,请提供更多上下文,我将尽力帮助您解决问题。
英文:
My Code:
#the first few lines are supposed to help reproduce the code if needed.
world_rank <- c(1, 2, 3, 4, 5, 6)
quality_of_education <- c(1, 9, 3, 2, 7, 13)
influence <- c(1, 3, 2, 6, 12, 13)
broad_impact <- c(1, 4, 2, 13, 9, 12)
patents <- c(3, 10, 1, 48, 15, 4)
university_matrix <- cbind(world_rank, quality_of_education, influence, broad_impact, patents)
rownames(university_matrix) <- c("harvard", "stanford", "MIT", "cambridge", "oxford", "Columbia")
usa_universities <- c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)
university_matrix[4,5] <- 3
#the following line is the actual problem.
university_matrix[usa_universities, "world_rank"] <- 2
What I expected to happen:
Specifically the last line. The line is supposed to " Replace all rankings for the USA’s universities (i.e., Harvard, Stanford, MIT, and Columbia) by 2. I tried this in R studio and it can produce the expected outcome which is a matrix with the world_rank column for harvard, stanford, mit and columbia to be 2.
This is the expected matrix: (formatting is off because of this interface)
world_rank quality_of_education influence broad_impact patents
harvard 2 1 1 1 3
stanford 2 9 3 4 10
MIT 2 3 2 2 1
cambridge 4 2 6 13 3
oxford 5 7 12 9 15
columbia 2 13 13 12 4
What actually happened:
Picture of the output: https://drive.google.com/file/d/1NU2jIiH1OvTPB16h5AvCCI-UlJ-sLq02/view?usp=sharing
#comment from DataQuest
The rankings for the USA's universities should be 2.
Question:
What have I done wrong here? The output of Data quest looks the same as R studio.
Thank you for your time in advance!
答案1
得分: 0
如果你想将所有排名都替换为 2
,那么不需要指定列名 - 这样所有列都会被设置为 "meant":
university_matrix[usa_universities, ] <- 2
university_matrix
world_rank quality_of_education influence broad_impact patents
harvard 2 2 2 2 2
stanford 2 2 2 2 2
MIT 2 2 2 2 2
cambridge 4 2 6 13 3
oxford 5 7 12 9 15
Columbia 2 2 2 2 2
或者,你也可以显式列出所有列名:
university_matrix[usa_universities, colnames(university_matrix)] <- 2
# 这与以下方式相同:
university_matrix[usa_universities, c("world_rank", "quality_of_education", "influence", "broad_impact", "patents")] <- 2
# 这与上述提到的方式相同:
university_matrix[usa_universities, ] <- 2
英文:
If you want to replace all rankings by 2
, then don't specify the column - so that all columns are "meant":
university_matrix[usa_universities, ] <- 2
university_matrix
world_rank quality_of_education influence broad_impact patents
harvard 2 2 2 2 2
stanford 2 2 2 2 2
MIT 2 2 2 2 2
cambridge 4 2 6 13 3
oxford 5 7 12 9 15
Columbia 2 2 2 2 2
Alternatively, you can also explicitely list all columns:
university_matrix[usa_universities, colnames(university_matrix)] <- 2
# which is the same like:
university_matrix[usa_universities, c("world_rank", "quality_of_education", "influence", "broad_impact", "patents")] <- 2
# which is the above proposed:
university_matrix[usa_universities, ] <- 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论