我有一个上面格式的数据文件.
我把它加载到R中,并尝试使用dist列中的值绘制直方图,我得到错误"x必须是数字".因此我尝试更改格式.
> head(data)
V1 V2
1 type gene_dist
2 A 64667
3 A 76486
4 A 97416
5 A 30876
6 A 88018
> summary(data)
V1 V2
A : 67 100 : 1
B :122 100906 : 1
type: 1 102349 : 1
1033 : 1
10544 : 1
10745 : 1
(Other):184
Run Code Online (Sandbox Code Playgroud)
我尝试使用列设置格式,sapply但值已更改:
> data[,2]<-sapply(data[,2],as.numeric)
> head(data)
V1 V2
1 type 190
2 A 146
3 A 166
4 A 189
summary(data)
V1 V2
A : …Run Code Online (Sandbox Code Playgroud) 我有一个数据框,应用融化函数后看起来类似于:
var val
1 a 0.6133426
2 a 0.9736237
3 b 0.6201497
4 b 0.3482745
5 c 0.3693730
6 c 0.3564962
Run Code Online (Sandbox Code Playgroud)
..................
初始数据框有3列,列名为a,b,c及其关联值.我需要在同一个图上绘图,使用ggplot为每个列(ecdf(a),ecdf(b),ecdf(c))提供相关的ecdf,但是我没有这样做.我试过了:
p<-ggplot(melt_exp,aes(melt_exp$val,ecdf,colour=melt_exp$var))
pg<-p+geom_step()
Run Code Online (Sandbox Code Playgroud)
但我得到一个错误:参数意味着不同的行数:34415,0.
有没有人知道如何做到这一点?该图应该看起来类似于绘图返回的图(ecdf(x)),而不是阶梯状图.
谢谢!
考虑以下向量:
[1] "1-1694429" "2-1546669" "3-928598" "4-834486" "5-802353" "6-659439" "7-552850"
"8-516804" "9-364061"
[10] "10-354181" "11-335154" "12-257915" "13-251310" "14-232313" "15-217628" "16-216569"
Run Code Online (Sandbox Code Playgroud)
我试图生成两个向量,每个向量包含通过分隔符" - "分割向量的每个元素而获得的值.
我用了:
f <- function(s) strsplit(s, "-")
cc<-sapply(names.reads, f)
Run Code Online (Sandbox Code Playgroud)
head(cc)$
1-1694429[1]"1""1694429"
$`2-1546669`
[1] "2" "1546669"
Run Code Online (Sandbox Code Playgroud)
我知道我可以访问它们,如:
> cc[[1]][1]
[1] "1"
> cc[[1]][2]
[1] "1694429"
Run Code Online (Sandbox Code Playgroud)
我想有两个向量,每个向量包含存储在cc[[i]][1]和的值 cc[[i]][2]...我可以不使用循环吗?(我有超过100万个元素)
我需要在以下示例中转义双引号并且R返回:
xx<-"the road is 'rocky all \"the\" way'"
xx
[1] "the road is 'rocky all \"the\" way'"
Run Code Online (Sandbox Code Playgroud)
最后一个字符串应包含单引号和双引号
the road is 'rocky all "the" way'
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我试图根据不同列上的最大值从数据框中删除重复的行
所以,对于数据框:
df<-data.frame (rbind(c("a",2,3),c("a",3,4),c("a",3,5),c("b",1,3),c("b",2,6),c("r",4,5))
colnames(df)<-c("id","val1","val2")
id val1 val2
a 2 3
a 3 4
a 3 5
b 1 3
b 2 6
r 4 5
Run Code Online (Sandbox Code Playgroud)
我想继续删除id的所有重复项,条件是对于相应的行,它们没有val2的最大值.
因此,数据框应成为:
a 3 5
b 2 6
r 4 5
Run Code Online (Sandbox Code Playgroud)
- >删除所有重复项但保留df $ val2最大值的行为子集(df,df $ id =="a")
我有一段代码提示用户输入,并在Windows中运行代码时完美地工作.但是,在Linux中,它执行每一行而不等待用户输入.
我已将代码添加到单独的函数中并使用系统("stty -echo")但未成功.为什么会发生这种情况,可以做些什么?(执行命令:Rscript test.R)
require(Biostrings)
read_value <- function(prompt_text = "", prompt_suffix = getOption("prompt"),
coerce_to= "character")
{
prompt <- paste(prompt_text, prompt_suffix)
system("stty -echo")
as(readline(prompt), coerce_to)
}
prints<-function()
{ opt<-as.character(readline(prompt = "Enter parameter values: "))
system("stty -echo")
i<-1
while ((i<=5))
{ if (i==1)
{ expr.filename <- as.character(readline(prompt = "Expression file name: "))
tryCatch( {expr.file<-read.table(expr.filename)},error=function(e)
{print("ERROR : Enter valid filename!") return })
}
if (i==2)
{ system("stty -echo")
fasta.filename <- as.character(readline(prompt = "Fasta file name: "))
tryCatch( {sequence_data<-read.DNAStringSet(fasta.filename)},error=function(e)
{print("ERROR : Enter valid filename!") …Run Code Online (Sandbox Code Playgroud) 附图(曼哈顿图)在x轴上包含来自基因组和Y轴-log(p)的染色体位置,其中p是与来自该特定位置的点(变体)相关的p值.

我使用以下R代码生成它(来自gap包):
require(gap)
affy <-c(40220, 41400, 33801, 32334, 32056, 31470, 25835, 27457, 22864, 28501, 26273,
24954, 19188, 15721, 14356, 15309, 11281, 14881, 6399, 12400, 7125, 6207)
CM <- cumsum(affy)
n.markers <- sum(affy)
n.chr <- length(affy)
test <- data.frame(chr=rep(1:n.chr,affy),pos=1:n.markers,p=runif(n.markers))
oldpar <- par()
par(cex=0.6)
colors <- c("red","blue","green","cyan","yellow","gray","magenta","red","blue","green", "cyan","yellow","gray","magenta","red","blue","green","cyan","yellow","gray","magenta","red")
mhtplot(test,control=mht.control(colors=colors),pch=19,bg=colors)
> head(test)
chr pos p
1 1 1 0.79296584
2 1 2 0.96675136
3 1 3 0.43870076
4 1 4 0.79825513
5 1 5 0.87554143
6 1 6 0.01207523
Run Code Online (Sandbox Code Playgroud)
我感兴趣的是将图的峰值坐标高于某个阈值(-log(p)).
考虑以下向量:
> v1
[1] 0.000000e+00 0.000000e+00 1.110223e-16 4.440892e-15 3.266195e-08 2.531242e-07
1.041204e-05 1.172329e-05 1.672447e-05 1.803203e-05
Run Code Online (Sandbox Code Playgroud)
我已将 mpfr 从函数(来自 Rmpfr 包)应用到 v1 :
> v2<-sapply(v1,function(x){mpfr(x,5)})
Run Code Online (Sandbox Code Playgroud)
v2 显然是一个列表,但我无法获取实际值。我尝试过使用 unlist、v2[[]]、转换为字符 v2[[]] 或向量..
> v2
[[1]]
'mpfr1' 0
[[2]]
'mpfr1' 0
[[3]]
'mpfr1' 1.11e-16
Run Code Online (Sandbox Code Playgroud) 如果我运行一个包含系统函数调用的R脚本,类似于:
x<-system("ls -t -r | tail -n 1") ,
Run Code Online (Sandbox Code Playgroud)
如果命令正确执行,x将为0,如果有错误,则x不为0.如何在执行脚本时保存R中终端的输出?
我试图通过字符串中第二次出现的字符进行拆分(在第二次出现字符之前返回字符串中的子字符串x)
对于字符串:
s <-"a_b_c",如果是分隔符"_",我需要子字符串:"a_b"
我的函数在第一次出现时返回子字符串:
return_topic<-function(s)
{
if (length(grep("_",s))>0)
{ return (unlist(strsplit(s,"_"))[1])}
else return (" ")
}
> return_topic("a_b_c")
[1] "a"
Run Code Online (Sandbox Code Playgroud)