修改矩阵中的元素 – R

huangapple go评论64阅读模式
英文:

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 &lt;- c(1, 2, 3, 4, 5, 6)
quality_of_education &lt;- c(1, 9, 3, 2, 7, 13)
influence &lt;- c(1, 3, 2, 6, 12, 13)
broad_impact &lt;- c(1, 4, 2, 13, 9, 12)
patents &lt;- c(3, 10, 1, 48, 15, 4)
university_matrix &lt;- cbind(world_rank, quality_of_education, influence, broad_impact, patents)
rownames(university_matrix) &lt;- c(&quot;harvard&quot;, &quot;stanford&quot;, &quot;MIT&quot;, &quot;cambridge&quot;, &quot;oxford&quot;, &quot;Columbia&quot;)
usa_universities &lt;- c(TRUE, TRUE, TRUE, FALSE, FALSE, TRUE)
university_matrix[4,5] &lt;- 3

#the following line is the actual problem.

university_matrix[usa_universities, &quot;world_rank&quot;] &lt;- 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, ] &lt;- 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)] &lt;- 2
# which is the same like:
university_matrix[usa_universities, c(&quot;world_rank&quot;, &quot;quality_of_education&quot;, &quot;influence&quot;, &quot;broad_impact&quot;, &quot;patents&quot;)] &lt;- 2
# which is the above proposed:
university_matrix[usa_universities, ] &lt;- 2

huangapple
  • 本文由 发表于 2023年2月16日 06:37:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定