小编vit*_*tor的帖子

从另一个中减去一个数据帧

假设我有一个更大的数据帧和一个更小的数据帧.如果较小的一个包含在较大的一个内,我怎么能有:

更大 - 更小

例:

小数据框:

     ID       CSF1PO CSF1PO.1 D10S1248 D10S1248.1 D12S391 D12S391.1
203079_BA_M     10       11       14         16      -9        -9
203079_BA_F      8       12       14         17      -9        -9
203080_BA_M     10       12       13         13      -9        -9
Run Code Online (Sandbox Code Playgroud)

大数据框架:

      ID      CSF1PO CSF1PO.1 D10S1248 D10S1248.1 D12S391 D12S391.1
203078_MG_M     -9       -9       15         15      18        20
203078_MG_F     -9       -9       14         15      17        19
203079_BA_M     10       11       14         16      -9        -9
203079_BA_F      8       12       14         17      -9        -9
203080_BA_M     10       12       13         13      -9        -9
203080_BA_F     10       11 …
Run Code Online (Sandbox Code Playgroud)

r

22
推荐指数
2
解决办法
3万
查看次数

更快的方法来比较数据框中的行

考虑下面的数据框架.我想比较每行与下面的行,然后采取相同的行超过3个值.

我编写了下面的代码,但是如果你有一个大数据框,它会非常慢.

我怎么能更快地做到这一点?

data <- as.data.frame(matrix(c(10,11,10,13,9,10,11,10,14,9,10,10,8,12,9,10,11,10,13,9,13,13,10,13,9), nrow=5, byrow=T))
rownames(data)<-c("sample_1","sample_2","sample_3","sample_4","sample_5")

>data
          V1 V2 V3 V4 V5
sample_1  10 11 10 13  9
sample_2  10 11 10 14  9
sample_3  10 10  8 12  9
sample_4  10 11 10 13  9
sample_5  13 13 10 13  9

output <- data.frame(sample = NA, duplicate = NA, matches = NA)
dfrow <- 1
for(i in 1:nrow(data)) {
    sample <- data[i, ]
    for(j in (i+1):nrow(data)) if(i+1 <= nrow(data)) {
    matches <- 0
        for(V in 1:ncol(data)) …
Run Code Online (Sandbox Code Playgroud)

r

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

如何根据第二个字符串删除字符串中的字符?

考虑这两个字符串:

string1 <- "GCTCCC...CTCCATGAAGTA...CTTCACATCCGTGT.CCGGCCTGGCCGCGGAGAGCCC"
string_reference <- "GCTCCC...CTCCATGAAGTATTTCTTCACATCCGTGT.CCGGCCTGGCCGCGGAGAGCCC"
Run Code Online (Sandbox Code Playgroud)

如何轻松删除"string1"中的点,但只删除"string_reference"中位于相同位置的点?

预期产量:

string1 = "GCTCCCCTCCATGAAGTA...CTTCACATCCGTGTCCGGCCTGGCCGCGGAGAGCCC"
Run Code Online (Sandbox Code Playgroud)

r

8
推荐指数
3
解决办法
304
查看次数

如何打印每个并排重复两次的数组元素?

请,我有以下代码行来编写文件的标题,但我想并排打印数组@order的每个元素.例如:$ 1 $ 1 $ 2 $ 2 $ 3 $ 3 ...我的输出文件的每一对列都有相同的名称.

print(join("\t", "Case_ID", "State", "Ind", "DoB", @order) . "\n");
Run Code Online (Sandbox Code Playgroud)

我可以做一些简单的事情,或者我必须在打印之前循环遍历数组以重复元素吗?

谢谢!

perl

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

使用tidyr :: separate与sep =“”将一列分成多个列

df <- data.frame(category = c("X", "Y"), sequence = c("AAT.G", "CCG-T"), stringsAsFactors = FALSE)

df
 category sequence
1        X     AAT.G
2        Y     CCG-T
Run Code Online (Sandbox Code Playgroud)

我想将该列sequence分为5列(每个字符一个)。我试图这样做,tidyr::separate但它在内部使用了stringi::stri_split_regex不接受空字符串作为分隔符的方式(尽管sep参数应使用正则表达式)。

library(tidyr)
separate(df, sequence, into = paste0("V", 1:5), sep="")

Error: Values not split into 5 pieces at 1, 2
In addition: Warning messages:
1: In stringi::stri_split_regex(value, sep, n_max) :
  empty search patterns are not supported
2: In stringi::stri_split_regex(value, sep, n_max) :
  empty search patterns are not supported
Run Code Online (Sandbox Code Playgroud)

预期的输出如下所示:

  category V1 …
Run Code Online (Sandbox Code Playgroud)

r tidyr

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

R中的图像功能

我正在尝试为初学者编程做一些有点复杂的事情.我有一个矩阵16x16,我想将值绘制为image()在R中使用的热图.如果总和(行索引+列索引)<= 15,我怎么能用蓝色绘制"0"(零)?那可能吗?

示例矩阵:

x <-  c(3045, 893, 692, 830, 617, 155, 246, 657, 105, 60, 18, 7, 7, 4, 2, 11234, 
2985, 2242, 2471, 1575, 366, 503, 1283, 170, 79, 32, 6, 4, 1, 3, 19475, 4756, 
3233, 3251, 1810, 409, 575, 1210, 139, 41, 11, 4, 2, 0, 0, 20830, 4739, 2990, 
2531, 1346, 298, 325, 612, 60, 17, 1, 0, 1, 0, 0, 15304, 3196, 1885, 1440, 610, 
117, 115, 185, 14, 2, 0, 0, 0, …
Run Code Online (Sandbox Code Playgroud)

r

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

非中心卡方概率和非中心性参数

如何获得非中心性参数的值,该参数为不同的临界值和自由度提供精确到0.9的概率?

例如,当显着性水平= 0.05和1自由度(临界值= 3.84)时,ncp必须等于10.50742才能获得0.9的概率:

1 - pchisq(3.841459, 1, 10.50742)
[1] 0.9
Run Code Online (Sandbox Code Playgroud)

r

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

了解ggplot2中的带宽平滑

realdata = https://www.dropbox.com/s/pc5tp2lfhafgaiy/realdata.txt

simulation = https://www.dropbox.com/s/5ep95808xg7bon3/simulation.txt

使用带宽= 1.5的数据密度图给出了以下图表:

prealdata = scan("realdata.txt")
simulation = scan("simulation.txt")
plot(density(log10(realdata), bw=1.5))
lines(density(log10(simulation), bw=1.5), lty=2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是使用ggplot2绘制相同的数据,带宽参数(adjust)似乎工作方式不同.为什么?

vec1 = data.frame(x=log10(realdata))
vec2 = data.frame(x=log10(simulation))
require(ggplot2)
ggplot() +
geom_density(aes(x=x, linetype="real data"), data=vec1, adjust=1.5) +
geom_density(aes(x=x, linetype="simulation"), data=vec2, adjust=1.5) +
scale_linetype_manual(name="data", values=c("real data"="solid", "simulation"="dashed"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

关于如何更好地平滑这些数据的建议也非常受欢迎!

r ggplot2

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

重新分配中is.integer()的行为

让我们创建一个整数向量,并为其第一个元素分配一个整数值,并将其重新赋值为非整数.

> int = integer(0)
> int[1]
> NA
> is.integer(int)
[1] TRUE

# assign int[1] to an integer
> int[1] = 1L
> is.integer(int)
[1] TRUE
# Now, re-assign to a non-integer
> int[1] = 1
> is.integer(int)
[1] FALSE # as expected
Run Code Online (Sandbox Code Playgroud)

现在,让我们做相同的但是以相反的顺序,即首先分配一个非整数,然后重新分配给一个整数.

# try again assigning to a non-integer first
> int = integer(0)
> int[1] = 1
> is.integer(int)
[1] FALSE
# Now, assigning to integer
> int[1] = 1L
> is.integer(int)
[1] FALSE # why? …
Run Code Online (Sandbox Code Playgroud)

r

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

rbind 2个不同长度的矢量名称

我有2个不同长度的向量:

vec1 <- rnorm(18, mean = 0.0018, sd = 0.0001)
names(vec1) <- c("CSF", "D10", "D13", "D16", "D18", "D1", "D21", "D22", "D3", "D5", "D7", "D8", "FGA", "PD", "PE", "TH", "TP", "vWA")

vec2 <- rnorm(20, mean = 0.0022, sd = 0.0002)
names(vec2) <-  c("CSF", "D10", "D12", "D13", "D16", "D18", "D19", "D1", "D21", "D22", "D2", "D2S", "D3", "D5", "D7", "D8", "FGA", "TH", "TP", "vWA") 
Run Code Online (Sandbox Code Playgroud)

我需要用他们的名字来训练这些向量.当另一个向量中的名称不存在时,应产生NA.

有一个简单的方法吗?

r

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

标签 统计

r ×9

ggplot2 ×1

perl ×1

tidyr ×1