问题标签 [rowsum]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
r - rowsum for matrix over specified number of columns in R
I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function but they are either giving me strange results or an error message. To illustrate, please see example code for an 8x8 matrix below. For the first row I need the sum of the row except item [1,1], for second row the sum except items [2,1] and [2,2] etc.
I have tried the following:
Alternatively:
As I understand rowSums needs an array rather than a vector (although not sure why). But I don't understand why the second way using sum doesn't work. Ideally, there is some way to only sum all columns in a row that lie above the diagonal.
Thanks a lot!
r - 对字符矩阵中的选定行求和:错误“x”在 R 中必须是数字
我是 R 的新手,这真的很基础,但它不适合我。我想对这个 data.frame 中 col 6:11 的行求和,例如:
输入(tab.res)
tab.res <-结构(c(“RIL”,“RIL”,“RIL”,“RIL”,“RIL”,“RIL”,“RIL”,“RIL”,“RIL”,“1993”,“ 1994“,”,“ 1995”,“ 1996”,“ 1998”,“ 2000”,“ 2003”,“ 2006”,“ 2006”,“ 2009”,“ 0.622648030237929”,“ 0.622780354568876 , "0.62229896748978", "0.619100952240793", "0.62242230375717", "0.621052639967347", "21.8279151394679", "18.558235100949", "19.1066487340136", "19.4658454721166", "19.7990392265612", "20.5366574203101", "22.3737846431613", "21.6823832708024", "22.5832057096871", "558.462585034014", "460.190476190476", "457.319727891156", "455.210884353742", "437.78231292517", "455.360544217687", "569.442176870748", "488.367346938776", "512.421768707483", "42.048152226551", "35.7094330210724" , "36.4311339004591", "35.8460590917321", "33.3991893243174", "33.9938868563961", "39.9166803633779", "34.7098561898221", "35.4769784708925", "44.0903088045558", "40.5555666070647", "41.419372563604", "43.5776682933757", "45.5922072021568", " 46.0154886164247", "47.3378504648163", "46.41940144389", "49.5580273139249", "40.6511227624141", "39.1466514500133", "40.4409858368967", "41.4903119307394", "42.9108185069093", "45.7459942527593", "47.7880868127877", "48.8291523581031", "48.3273707080036", "31.3644667603767", "31.0971941969479", "32.4804982013221" , "32.4553994494653", "34.2369548403931", "35.6674106248615", "38.53520691726", "38.8160980443", "40.8055122694451", "24.5415097525631", "23.5956393932039", "23.6298291129159", "24.6786066881199", "25.3372707363408", "25.7293935423119", " 26.2842616375337”、“29.2438109789201”、“29.6350482936186”、“76.6236464549925", "44.5672068727091", "46.962777170159", "48.0267179639631", "51.298941016064", "53.5121260383574", "55.4511984380194", "53.5652556461721", "55.2830178740705"), .Dim = c(9L, 11L), .Dimnames = list (NULL,c(“治疗”,“年”,“WD”,“BA”,“StemD”,“AGB.10.20”,“AGB.20.30”,“ABG.30.40”,“ABG.40.50”,“ ABG.50.60"、"ABG.60.+")))Dimnames = list(NULL, c("Treat", "Year", "WD", "BA", "StemD", "AGB.10.20", "AGB.20.30", "ABG.30.40", "ABG.40.50 ", "ABG.50.60", "ABG.60.+")))Dimnames = list(NULL, c("Treat", "Year", "WD", "BA", "StemD", "AGB.10.20", "AGB.20.30", "ABG.30.40", "ABG.40.50 ", "ABG.50.60", "ABG.60.+")))
这应该解决了这个问题:
tab.res$total <- rowSums(subset(tab.res, select=6:11))
但我不断得到:
rowSums(subset(tab.res, select = 6:11)) 中的错误:“x”必须是数字
我尝试使用“as.numeric”来转换出现错误的任何内容,但我没有得到任何结果......有人打电话告诉我问题出在哪里?
r - R 的 rowsum 疑难解答
我正在对 R 的行求和功能进行故障排除。我有以下称为总计的向量:
该向量有 20 个不同的类别,我想将每个类别的所有值相加。我的代码是:
但我收到以下错误:
我有点困惑,因为在文档中,“组”是一个向量/因子,它给出了每行 x 一个元素的分组。
任何帮助,将不胜感激。
谢谢!
r - R中的行和列总和
这是我的数据集 ( MergedData
) 在 R 中的样子的示例,其中我的每个参与者(5 行)在每次测试(7 列)中获得了一个分数。我想知道所有测试的总分(所有列),但每个参与者(行)。
此外,我的完整数据集不仅仅是这几个变量,所以如果可能的话,我想使用公式和循环来完成,而不必逐行/逐列输入。
我已经尝试过了,但它不起作用:
我收到以下错误消息:
我该如何解决这个问题?谢谢!!
r - 对角线上/下矩阵的行和
我想计算上/下三角矩阵(对角线)的行/列和。例子:
我知道这可以通过简单的for
循环来完成,但我想避免这种情况。我正在寻找纯粹的R
解决方案。
r - 循环中交替行的 rowSums
我有一个数据框,称为Paper
每年的论文引用,包括他们的出版年份以及一些元数据(期刊、作者)。看起来像:
我想计算出版两年后的引用总和,并将此列表附加到Paper
. 但是,我对每一年都不感兴趣,只对列表中指定的那些感兴趣Years
。我的步骤(下面的代码)如下: Order Paper
acc。Publication.Year
,选择Publication.Year
第一年的 X 行(即1999X2000
年X2001
),计算总和,将总和绑定在一起,cbind 到Paper
.
是否有(更多)优雅的方式来做到这一点?
r - 获取数据框中多列的成对总和
我有一个看起来像这样的数据框:
x<-data.frame(a=6, b=5:1, c=7, d=10:6)
我正在尝试在另一个数据框中获取列a
&b
和c
&的总和,d
它应该如下所示:
我试过这个rowSums()
函数,但它返回每行所有列的总和,我试过rowSums(x[c(1,2), c(3,4)])
但没有任何效果。请帮忙!!
r - 排除R中rowsum范围内的值
我正在尝试获取数据框每一行的行总和,但有一个条件。我想在应用 log2 之后排除所有介于 -1 和 1 之间的值。我知道如何排除 NA,但我对排除实际数字感到困惑。我的数据框只是数字,除了行名和列名。
r - Count occurrences of a string, by row, in a large data frame
I am trying to count a binary character outcome by row in a large data frame:
What I need to know is the frequency of wins and losses by row. This is just a short example (fragment of large simulated output) but for row 1, in five simulations, I have five Losses, row two three loss and two win, etc.
I was hoping to generate either a separate table that shows the frequency of wins/loss by row or, if that won't work, add two new columns: one that provides the number of "Win" and "Loss" for each row.
Each row is a different case, and each column is a replicate of that case. This appears as a data frame of factors with two levels "Loss" "Win".
r - 计算数据框中前两列的个数
说,我有一个如下的数据框,有三列。我想创建第四列,其值是前两列中的个数的总和
迭代版本
输入
预期产出