假设我有一个像这样的矩阵:
matrix1 = [[11,12,13,14,15,16,17],[21,22,23,24,25,26,27],[31,32,33,34,35,36,37],
[41,42,43,44,45,46,47],[51,52,53,54,55,56,57],[61,62,63,64,65,66,67],
[71,72,73,74,75,76,77]]
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,它将接收两个矩阵并进行逐点乘法.(不使用numpy)
我已经看到使用拉链的一些东西,但这似乎并不适合我.我认为这是因为我的列表是列表而不是单个列表.
我的代码:
def pointwise_product(a_matrix, a_second_matrix):
# return m[i][j] = a_matrix[i][j] x a_second_matrix[i][j]
return [i*j for i,j in zip(a_matrix,a_second_matrix)]
Run Code Online (Sandbox Code Playgroud)
Matrix1可以作为两个参数插入.另一个名为display_matrix的函数将接受此函数并在新行上显示列表的每个元素,但这超出了本问题的范围.
我的猜测是我需要一些列表推导或lambda函数,但我对python来说太新了以至于完全掌握它们.
我正在尝试实现一个函数来使用单个函数绘制两个时间序列图,但它只返回一个这里是代码有什么问题
visualize <- function(x) {
x<-data.frame(x)
x1 <- x[x$chr == 1, ]
x2 <- x[x$chr != 1, ]
t1<-data.frame("range"=1:nrow(x1))
t2<-data.frame("range"=1:nrow(x2))
t1$testsample_first<-exp(x1$testSample1)
t1$testsample_second<-exp(x1$testSample2)
t2$testsample_first<-exp(x2$testSample1)
t2$testsample_second<-exp(x2$testSample2)
dygraph(t1);dygraph(t2)
}
(visualize(scon))
Run Code Online (Sandbox Code Playgroud)
它只绘制第二个。我试图通过在第一个但相同的结果中调用第二个函数来实现。
下面的链接讨论了如何使用gets3methods
调用来构建一个钩子to.basic
.然而,这仅适用于可以分解为更简单的geoms(geom_line
to geom_path
)的geoms .因为geom_text_repel
我不确定是否可能.
它继续提到你必须为它创建一个自定义方法geom2trace
.怎么会这样做呢?我是S3数据指南的新手并做了类似的事情,所以我不确定从哪里开始.
如果我跑了methods(geom2trace)
我得到:
> methods(geom2trace)
[1] geom2trace.default* geom2trace.GeomBar* geom2trace.GeomBlank* geom2trace.GeomBoxplot* geom2trace.GeomErrorbar*
[6] geom2trace.GeomErrorbarh* geom2trace.GeomPath* geom2trace.GeomPoint* geom2trace.GeomPolygon* geom2trace.GeomText*
[11] geom2trace.GeomTile*
see '?methods' for accessing help and source code
Run Code Online (Sandbox Code Playgroud)
我看到geom2trace.GeomText
这里似乎需要的东西.
在我尝试创建交互式wordcloud时,这里的任何指导都将是一个巨大的帮助!很高兴分享这个项目的成果.
在更复杂的情况下,您的自定义geom无法转换为较低级别的geom,需要geom2trace()泛型的自定义方法(方法(geom2trace)列出我们原生支持的所有基本geom).此方法应涉及从数据框到符合plotly.js图引用的类似列表的对象的转换.
https://plotly-book.cpsievert.me/translating-custom-ggplot2-geoms.html
假设我有以下文本输出。使用 Quanteda kwic()
,我想找到匹配模式的逐字逐句。我希望能够将输出显示为单行而不是 3 列,因此我将它们重新整形并将它们合并在一起。我希望能够突出显示字符串中的模式词。这完全可行吗?如果是这样,如何?
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
fluidRow(DT::dataTableOutput("table1"))
)
)
server= function(input,output){
output$table1 = renderDataTable({
y = kwic(x = data_corpus_inaugural,pattern = 'united',window = 10,valuetype = 'regex')
y = as.data.table(y)
y = y[,4:6]
y$new = do.call('paste',y)
y = y[,!c(1:3)]
})
}
shinyApp(ui,server)
Run Code Online (Sandbox Code Playgroud) 我试图通过单指数"衰减"来拟合下面数据中的一些曲线.图形显示并不像拉出时间常数那么重要.y轴是pA,x是以秒为单位的时间.
dput(stackover_data)
structure(list(Time = c(0.09990001, 0.19990001, 0.29990001, 0.39990001,
0.49990001, 0.59990001, 0.69990001, 0.79990001, 0.89990001, 0.99990001,
1.09990001, 1.19990001, 1.29990001, 1.39990001, 1.49990001, 1.59990001,
1.69990001, 1.79990001, 1.89990001, 1.99990001, 2.09990001, 2.19990001,
2.29990001, 2.39990001, 2.49990001, 2.59990001, 2.69990001, 2.79990001,
2.89990001, 2.99990001, 3.09990001, 3.19990001, 3.29990001, 3.39990001,
3.49990001, 3.59990001, 3.69990001, 3.79990001, 3.89990001, 3.99990001,
4.09990001, 4.19990001, 4.29990001, 4.39990001, 4.49990001, 4.59990001,
4.69990001, 4.79990001, 4.89990001, 4.99990001, 5.09990001, 5.19990001,
5.29990001, 5.39990001, 5.49990001, 5.59990001, 5.69990001, 5.79990001,
5.89990001, 5.99990001, 6.09990001, 6.19990001, 6.29990001, 6.39990001,
6.49990001, 6.59990001, 6.69990001, 6.79990001, 6.89990001, 6.99990001,
7.09990001, 7.19990001, 7.29990001, 7.39990001, 7.49990001, 7.59990001, …
Run Code Online (Sandbox Code Playgroud) 我有一个如下所示的文件:
1 2 3 4 5 6 7
1 0 1 1 1 1 1 1
2 0 0 1 1 1 1 1
3 0 0 0 1 1 1 1
4 0 0 0 0 1 1 1
5 0 0 0 0 0 1 1
6 0 0 0 0 0 0 1
7 0 0 0 0 0 0 0
Run Code Online (Sandbox Code Playgroud)
我只想读入 1 和 0 并忽略顶部标题行和行名称(第一列)。
到目前为止,我已经设置了标题行,但如何跳过跳过列。到目前为止我的代码
with open('file') as f:
next(f) #skips header row
content = …
Run Code Online (Sandbox Code Playgroud) 我有一张看起来像这样的桌子。
> dput(theft_loc)
structure(c(13704L, 14059L, 14263L, 14450L, 14057L, 15503L, 14230L,
16758L, 15289L, 15499L, 16066L, 15905L, 18531L, 19217L, 12410L,
13398L, 13308L, 13455L, 13083L, 14111L, 13068L, 19569L, 18771L,
19626L, 20290L, 19816L, 20923L, 20466L, 20517L, 19377L, 20035L,
20504L, 20393L, 22409L, 22289L, 7997L, 8106L, 7971L, 8437L, 8246L,
9090L, 8363L, 7934L, 7874L, 7909L, 8150L, 8191L, 8746L, 8277L,
27194L, 25220L, 26034L, 27080L, 27334L, 30819L, 30633L, 10452L,
10848L, 11301L, 11494L, 11265L, 11985L, 11038L, 12104L, 13368L,
14594L, 14702L, 13891L, 12891L, 12939L), .Dim = c(7L, 10L), .Dimnames = structure(list( …
Run Code Online (Sandbox Code Playgroud) 数据
我有一个列表列表,看起来像这样:
sublist1 <- list(power=as.matrix(c(rnorm(10)),c(rnorm)),x=rnorm(10),y=rnorm(10))
sublist2 <- list(power=as.matrix(c(rnorm(10)),c(rnorm)),x=rnorm(10),y=rnorm(10))
sublist3 <- list(power=as.matrix(c(rnorm(10)),c(rnorm)),x=rnorm(10),y=rnorm(10))
mylist = list(sublist1,sublist2,sublist3)
Run Code Online (Sandbox Code Playgroud)
我的目标是仅提取名为的矩阵 power
我试过了
mylist_power =mylist[sapply(mylist, '[', 'Power')]
Run Code Online (Sandbox Code Playgroud)
但那不起作用.
布朗尼点警报!
如何找到新创建的矩阵列表的平均值?
披露:我不确定如何为这个问题制作一个可重复的例子.
我正在尝试使用gridExtra
包绘制一个grobs列表.
我有一些看起来像这样的代码:
## Make Graphic Objects for Spec and raw traces
for (i in 1:length(morletPlots)){
gridplots_Spec[[i]]=ggplotGrob(morletPlots[[i]])
gridplots_Raw[[i]]=ggplotGrob(rawPlot[[i]])
gridplots_Raw[[i]]$widths=gridplots_Spec[[i]]$widths
}
names(gridplots_Spec)=names(morletPlots)
names(gridplots_Raw)=names(rawPlot)
## Combine spec and Raw traces
g=list()
for (i in 1:length(rawPlot)){
g[[i]]=arrangeGrob(gridplots_Spec[i],gridplots_Raw[i],heights=c(4/5,1/5))
}
numPlots = as.numeric(length(g))
##Plot both
for (i in 1:numPlots){
grid.draw(g[i],ncol=2)
}
Run Code Online (Sandbox Code Playgroud)
让我来看看代码.
morletPlots
= ggplots列表
rawplot
= ggplots列表
gridplots_spec
和gridplots_Raw
=上面的ggplots中的grobs列表.
g
=上面两个grobs的列表,如此组合gridplots_spec[1]
,gridplots_raw[1]
依此类推等等列表的长度.
现在我的目标是两个将所有这些分为两列.但每当我通过gridplots_spec[i]
grid.draw循环时,我都会收到一个错误:
Error in UseMethod("grid.draw") :
no applicable method for 'grid.draw' applied to …
我有这样的字符串
Allen, Mr. William Henry
Run Code Online (Sandbox Code Playgroud)
而且我想抓住'先生'部分.
我试过这个
[,](.*?)[.]
Run Code Online (Sandbox Code Playgroud)
但这抓住了'先生'
我怎么才能抓住'先生'部分?
我有这样的数据
> dput(testdat)
structure(list(Type = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Saline",
"Compound 1"), class = "factor"), Treatment = structure(c(1L,
2L, 3L, 4L, 6L, 5L), .Label = c(".0032uM", ".016uM", ".08uM",
".4uM", "2uM", "10uM"), class = "factor"), Peak = c(1071.28430020209,
1458.23366806524, 2714.49856342393, 3438.83453920159, 3938.86391759534,
2980.10159109856), Area1 = c(3312.99749863082, 4798.35142770291,
9044.21362002965, 11241.1497514069, 11575.3444645068, 9521.69011119236
), SS1 = c(781.759834505516, 1191.6273298958, 2180.02082601411,
2601.33855989239, 2492.11886600804, 2185.39715502702), Conc = c(0.0032,
0.016, 0.08, 0.4, 10, 2), logconc = c(-2.49485002168009, -1.79588001734408,
-1.09691001300806, -0.397940008672038, 1, 0.301029995663981),
Conc_nm …
Run Code Online (Sandbox Code Playgroud) 如果我有这样的矩阵:
x = rand(256,144160);
Run Code Online (Sandbox Code Playgroud)
我怎样才能将它分成更小的矩阵256 x 901
?
我尝试了mat2cell(x,256,901)
但是我收到了这个错误:
输入参数
D1
throughD2
必须与输入矩阵大小的每个维度相加,[256 144160].'
这不是要求新代码,而是更多关于R如何进行这样的计算.当然,我会采取任何和所有建议来提高效率.
假设我有一个这样的脚本:
x=matrix(complex(1:10,1:10,imaginary = 1:10),ncol=2)
y=x+300
raw=list(x,y)
raw_complex = list(raw,raw,raw,raw)
Run Code Online (Sandbox Code Playgroud)
这是一个复杂矩阵列表.我正试图从中获得相角,这是:phase = atan(Im(x)/Re(x))
我目前的代码是:
for (m in 1:length(raw_complex)){
for (n in 1:length(raw_complex[[m]])){
for (i in 1:dim(raw_complex[[m]][[n]])[1]){
for (j in 1:dim(raw_complex[[m]][[n]])[2]){
raw_complex[[m]][[n]][i,j]=(atan(Im(raw_complex[[m]][[n]][i,j])/Re(raw_complex[[m]][[n]][i,j])))
}}}}
Run Code Online (Sandbox Code Playgroud)
我知道,我知道,避免在R中使用循环.但从概念上讲,这使我更容易看到正在发生的事情,而不是lapply或sapply.
我的问题是,在循环的每次迭代中,R是在内存中复制整个列表或矩阵而不是一次拉出一个单独的元素?显然,我宁愿每次迭代都没有R制作完整的副本.
我的真实数据集有一个4的列表,在列表的每个元素中有95个矩阵.每个矩阵都是145x901,所以你可以看到我希望如何尽可能快.
哦,如果输出是实数而不是复数,那就太好了.我已尝试添加前面as.numeric()
的atan()
但似乎没有帮助.
谢谢!