小编mon*_*nic的帖子

如何对R中的列进行滚动求和?

roll_sum 和许多其他方法(例如 https://vandomed.github.io/moving_averages.html)仅用于对行求和。我有一个很大的矩阵,我没有足够的内存来转置它。有没有办法可以直接对列进行 roll_sum ?

例如:

library(roll)

A=matrix(rnorm(10000),100)
roll_sum(A,3)
Run Code Online (Sandbox Code Playgroud)

但我想跨列执行此操作。


接下来,到目前为止所有的方法都是在不使用多核处理的情况下实现的。任何人都可以提供具有此功能的解决方案吗?

r matrix linear-algebra

9
推荐指数
2
解决办法
466
查看次数

在 r 中创建不等维的对角矩阵

有没有diag()类似的方法在 r 中创建不等维对角矩阵?例如:

> diag(rep(1,9), nrow=3)
 
1 1 1 0 0 0 0 0 0
0 0 0 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1
Run Code Online (Sandbox Code Playgroud)

r matrix

4
推荐指数
1
解决办法
135
查看次数

创建非对角线/移动窗口矩阵

如何用 R 创建非对角线/移动窗口矩阵?

> offdiag(3, 4, 6)

1 1 1 0 0 0
0 1 1 1 0 0
0 0 1 1 1 0
0 0 0 1 1 1 
Run Code Online (Sandbox Code Playgroud)

r matrix

4
推荐指数
1
解决办法
134
查看次数

index out of bound when iterrow() how is this possible?

I got error message:

5205
(5219, 25)
5221
(5219, 25)
Traceback (most recent call last):
  File "/Users/Chu/Documents/dssg2018/sa4.py", line 44, in <module>
    df.loc[idx,word]=len(df.iloc[indices[idx]][df[word]==1])/\
IndexError: index 5221 is out of bounds for axis 0 with size 5219
Run Code Online (Sandbox Code Playgroud)

when I'm traversing the data frame, the index comes from the iterators. I don't know how is this even possible? idx directly comes from the dataframe

bt = BallTree(df[['lat','lng']], metric="haversine")
indices = bt.query_radius(df[['lat','lng']],r=(float(10)/40000)*360)

for idx,row in df.iterrows():
    for word in bag_of_words:
        if word in row['caption']: …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

3
推荐指数
1
解决办法
886
查看次数

如何将不等长的向量列表转换为二进制数据帧?

如何将不等长的向量列表转换为二进制数据帧?我的数据集如下所示:

> gene_annot
[[1]]
 [1] "lipid binding"           "catalytic activity"      "hydrolase activity"      "lipid metabolic process"
 [5] "cytosol"                 "organelle"               "mitochondrion"           "signaling"              
 [9] "extracellular region"    "extracellular space"    

[[2]]
[1] "extracellular region" "extracellular space"  "organelle"           

[[3]]
[1] "extracellular region" "extracellular space" 

[[4]]
logical(0)

[[5]]
[1] "organelle"                          "nucleus"                            "nucleoplasm"                       
[4] "immune system process"              "defense response to other organism" "protein folding" 
Run Code Online (Sandbox Code Playgroud)

我想为每个标签创建一列,每个单元格都包含一个二进制变量,指示该标签是否出现在该行中。我怎样才能在 R 中做到这一点?例如,我期望这样的数据框:

> gene_annot_binary
lipid binding extracellular region

1             1

0             1

0             1

0             0

0             0
Run Code Online (Sandbox Code Playgroud)

r dplyr

2
推荐指数
1
解决办法
67
查看次数

`quantile()` 组成数字

为什么

quantile(c(0.1,0.2))
Run Code Online (Sandbox Code Playgroud)

   0%   25%   50%   75%  100% 
0.100 0.125 0.150 0.175 0.200 
Run Code Online (Sandbox Code Playgroud)

而实际上数据集中没有值 0.125、0.150 和 0.175?

r

1
推荐指数
1
解决办法
50
查看次数

映射 R 中列表中的元素对

如果我有一个对称二元运算符,我想将其应用于列表中的元素对,有没有一种简单的方法可以在 R 中做到这一点?我试过:

A <- list(1,2,3)
mapply(function(x,y) x+y, A,A)
Run Code Online (Sandbox Code Playgroud)

但这只给出了x[n]+y[n]所有内容n=1..N,但我希望x[n]+y[m]所有内容都m=1..n, n=1..N作为列表返回。outer(..)这样做涉及m=1..N, n=1..N冗余计算,所以我想打折它。

请注意,我不需要这个简单示例的解决方案。我需要一个也适用于非数字输入的通用解决方案。我想做的事情是这样的:

mapply(function(set_1, set_2) setequal(intersect(set_1, set_2),  set_3), list_of_sets, list_of_sets)
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,加法和交集都是对称的。在第一个示例中,我期望list(3,4,5)list(1+2,1+3,2+3). 对于第二种情况,我的输入list_of_sets是:

> list_of_sets
[[1]]
numeric(0)

[[2]]
[1] 1

[[3]]
[1] 2

[[4]]
[1] 1 2

[[5]]
[1] 3

[[6]]
[1] 1 3

[[7]]
[1] 2 3

[[8]]
[1] 1 2 3
Run Code Online (Sandbox Code Playgroud)

set_3作为c(1,2)一个简单的例子。

r mapply

-1
推荐指数
1
解决办法
179
查看次数

标签 统计

r ×6

matrix ×3

dataframe ×1

dplyr ×1

linear-algebra ×1

mapply ×1

pandas ×1

python ×1