非常感谢你的帮助!
我正在尝试修改现有矩阵,以便在将新行添加到矩阵时,它会从预先存在的矩阵中删除值.
例如,我有矩阵:
[,1] [,2] [,3] [,4]
1 1 0 0
0 1 0 0
1 0 1 0
0 0 1 1
Run Code Online (Sandbox Code Playgroud)
我想添加另一个矢量,I.vec,它有两个值(I.vec=c(0,1,1,0)).这很容易做到.我只是将它与矩阵联系起来.现在,对于I.vec等于1的每一列,我想从其他行中随机选择一个值并使其为零.理想情况下,这将最终得到一个矩阵,如:
[,1] [,2] [,3] [,4]
1 0 0 0
0 1 0 0
1 0 0 0
0 0 1 1
0 1 1 0
Run Code Online (Sandbox Code Playgroud)
但是每次运行迭代时,我都希望它再次随机采样.
所以这就是我尝试过的:
mat1<-matrix(c(1,1,0,0,0,1,0,0,1,0,1,0,0,0,1,1),byrow=T, nrow=4)
I.vec<-c(0,1,1,0)
mat.I<-rbind(mat1,I.vec)
mat.I.r<-mat.I
d1<-mat.I[,which(mat.I[5,]==1)]
mat.I.r[sample(which(d1[1:4]==1),1),which(mat.I[5,]==1)]<-0
Run Code Online (Sandbox Code Playgroud)
但这只删除了我想要删除的两个值中的一个.我也尝试过对矩阵进行子集化的变化,但我还没有成功.
再次感谢你!
我想使用filter()R 中的函数处理缺失值。
事实上,我希望计算包含缺失值的经典时间序列在X_t = 1/(2*T+1) * sum(X_i, i = (t-T)...(t+T))哪里。计算时间间隔内的总和,但不给出除 s之外的值的平均值。(X_t)filter()[(t-T);(t+T)]NA
有谁知道如何处理这个问题吗?
我想手动保存我的标签分隔文件.我的意思是我希望用户在他想要保存数据时选择目录和文件名.(例如,我已将单个文件合并为单个文件,并希望保存它.)
通常我会使用write.table但在write.table我们定义该函数中的目录路径和文件名但我想要一个函数,用户可以在其所需目录中保存任何名称的文件.
这个问题与我关于Roxygen的问题有关.
我想编写一个新的函数,用于对字符串进行自动换行,类似于strwrap或者stringr::str_wrap,但有以下几种情况:不能允许字符串中用引号括起来的任何元素(子字符串).
因此,例如,使用以下示例数据
test <- "function(x=123456789, y=\"This is a long string argument\")"
cat(test)
function(x=123456789, y="This is a long string argument")
strwrap(test, width=40)
[1] "function(x=123456789, y=\"This is a long"
[2] "string argument\")"
Run Code Online (Sandbox Code Playgroud)
我希望a的所需输出为newWrapFunction(x, width=40, ...):
desired <- c("function(x=123456789, ", "y=\"This is a long string argument\")")
desired
[1] "function(x=123456789, "
[2] "y=\"This is a long string argument\")"
identical(desired, newWrapFunction(tsring, width=40))
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
你能想到办法吗?
PS.如果你可以帮我解决这个问题,我会建议将这段代码作为补丁roxygen2.我已经确定了应该应用此补丁的位置,并将确认您的贡献.
在ggplot生成的图中,沿x轴的每个标签都是一个字符串,即"1990年的产品".但是,生成的绘图在每个单词之间存在一段时间.换句话说,上面的字符串显示为"the.product.in.1990"
如何确保不添加上述"."?
以下代码是我用于沿x轴为每个点添加字符串的代码
last_plot()+scale_x_discrete(limits=ddata$labels$text)
Run Code Online (Sandbox Code Playgroud)
示例代码:
library(ggdendro)
x <- read.csv("test.csv",header=TRUE)
d <- as.dist(x,diag=FALSE,upper=FALSE)
hc <- hclust(d,"ave")
dhc <- as.dendrogram(hc)
ddata <- dendro_data(dhc,type="rectangle")
ggplot(segment(ddata)) + geom_segment(aes(x=x0,y=y0,xend=x1,yend=y1))
last_plot() + scale_x_discrete(limits=ddata$labels$text)
Run Code Online (Sandbox Code Playgroud)
每一行ddata$labels$text都是一个字符串,就像"1990年的产品".我想在生成的图中保留相同的格式而不是"the.product.in.1990"
我正在使用以下脚本分析大量数据:
M <- c_alignment
c_check <- function(x){
if (x == c_1) {
1
}else{
0
}
}
both_c_check <- function(x){
if (x[res_1] == c_1 && x[res_2] == c_1) {
1
}else{
0
}
}
variance_function <- function(x,y){
sqrt(x*(1-x))*sqrt(y*(1-y))
}
frames_total <- nrow(M)
cols <- ncol(M)
c_vector <- apply(M, 2, max)
freq_vector <- matrix(nrow = sum(c_vector))
co_freq_matrix <- matrix(nrow = sum(c_vector), ncol = sum(c_vector))
insertion <- 0
res_1_insertion <- 0
for (res_1 in 1:cols){
for (c_1 in 1:conf_vector[res_1]){
res_1_insertion <- res_1_insertion …Run Code Online (Sandbox Code Playgroud) 我对R很新,所以如果我的问题不清楚,请耐心等待.
我有一个data.frame有5列的"蛋白质",即;
1.protein_name,2.protein_FC,3.protein_pval,4.mRNA_FC,5.mRNA_pval和6.freq.
我试图绘制一个火山图,其中x = log2(protein_FC),y = -log10(protein_pval).然后将点的大小映射到频率和颜色到mRNA_FC.这一切都很好,这是我用过的代码:
ggplot( protein [ which ( protein$freq <= 0.05 ),] , aes( x = log2( protein_FC ) ,
y = -log10 ( protein_pval ) , size = freq , colour = mRNA_FC ,
label = paste(protein_name,",",mRNA_pval), alpha=1/1000)) +
geom_point() + geom_text( hjust = 0 , vjust = 0 , colour = "black" , size = 2.5 ) +
geom_abline( intercept = 1.3 , slope = 0) +
scale_colour_gradient(limits=c(-3,3))
Run Code Online (Sandbox Code Playgroud)
一切都很好,直到这里.但由于实验的性质,数据非常密集mRNA_FC = …
我有以下ggplot2代码,我想使用基本图形而不是ggplot2来生成类似的输出 - 但我似乎无法找到一种方法来区分多个"属性"与正常情节.我错过了什么:
GGPLOT2:
ggplot(data.df, aes(x=Axis1, y=Axis2, shape=Plant, color=Type)) +
geom_point()
Run Code Online (Sandbox Code Playgroud)
我的情节尝试(内联帮助让我有点相似):
data.ma <- as.matrix(data.df)
plot(range(data.ma[,6]), range(data.ma[,7]),xlab="Axis 1",ylab="Axis 2")
points(data.ma[data.ma[,1] == 'Plant1',6],
data.ma[data.ma[,1] == 'Plant1',7], pch=2)
points(data.ma[data.ma[,1] == 'Plant2',6],
data.ma[data.ma[,1] == 'Plant2',7], pch=3)
legend(0,legend=c("Plant1","Plant2"))
Run Code Online (Sandbox Code Playgroud)
这给了我一个图,其中至少可以在图中区分"植物"类型,但它确实看起来很复杂,我无法弄清楚如何根据"类型"行更改所有点的颜色.
有什么建议?
编辑 - 一个数据示例//我意识到我第一次尝试使用情节甚至没有给出正确的例子:(:
library(ggplot2)
data.df <- data.frame(
Plant=c('Plant1','Plant1','Plant1','Plant2','Plant2','Plant2'),
Type=c(1,2,3,1,2,3),
Axis1=c(0.2,-0.4,0.8,-0.2,-0.7,0.1),
Axis2=c(0.5,0.3,-0.1,-0.3,-0.1,-0.8)
)
ggplot(data.df, aes(x=Axis1, y=Axis2, shape=Plant, color=Type)) +
geom_point()
data.ma <- as.matrix(data.df)
plot(range(data.ma[,3]), range(data.ma[,4]),xlab="Axis 1",ylab="Axis 2")
points(data.ma[data.ma[,1] == 'Plant1',3],
data.ma[data.ma[,1] == 'Plant1',4], pch=2)
points(data.ma[data.ma[,1] == 'Plant2',3],
data.ma[data.ma[,1] == 'Plant2',4], pch=3)
legend(0,legend=c("Plant1","Plant2"))
Run Code Online (Sandbox Code Playgroud) 为了在匹配过程之前和之后直观地显示协变量平衡,我编写了以下代码:
library(lattice)
library(gridExtra)
StandBias.df= data.frame(
Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
Groups = factor(c(rep("0", 6), rep("1", 6))),
Values = c(0.26, 0.4, 0.3, 0.21, 0.6, 0.14, 0.12, -0.04, -0.23, 0.08, 0.14, -0.27))
d1<-dotplot(Category ~Values, data = StandBias.df, groups = Groups,
main = "Standardized Mean Differences", col = c("black", "grey50"), pch=c(22,15), xlab=NULL,
key=list(text=list(c("Pre-Matching", "Post-Matching")),
points=list(col = c("black", "grey50"), pch=c(22,15)),
space="bottom", border=T))
Ttest.df = data.frame(
Category= rep(c("A", "B", "C", "D", "E", "F"), 2),
Groups = factor(c(rep("0", 6), rep("1", 6))),
Values = …Run Code Online (Sandbox Code Playgroud) staticdocs是一个Hadley Wickham软件包,可以为软件包创建漂亮的网页.我已经非常成功地使用它(虽然还没有弄清楚如何在没有错误的情况下添加示例)但每次我必须手动重新格式化index.html创建的文件的自述文件部分.这似乎很愚蠢,因为它从ggplot2 staticdocs使用中看出,有一种方法可以在里面的索引文件中设置自述文件inst/staticdocs/.
现在Hadley还没有记录好的软件包(staticdocs),我不确定如何更改索引文件中的readme参数/列表条目.有没有人有关于这种格式应该是什么样子的指导; 即,条目的外观格式?