apply

R: converting JSON time format into POSIX

时光怂恿深爱的人放手 提交于 2019-12-08 01:03:51
问题 I have a JSON character string that I put into a data frame. I am able to do it, but I am having trouble using one of the apply functions to convert all the time character strings into POSIX format. See here for more background on that. The JSON time format is: %h-%m-%dT%H:%M:%S- 2012-01-29T17:00:45-11:00 Lets say I have a data frame as shown: .Close .High .Low .Open Time 1 5.517339 5.539509 5.404098 5.495318 2012-01-30T12:00:45+08:00 2 5.485943 5.521242 5.467357 5.467641 2012-01-30T11:00:45

addEventListener using apply()

早过忘川 提交于 2019-12-08 00:26:23
问题 I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){ return function(){ console.log( 'add something to ' + old.name ); old.apply(this, arguments); } } addEventListener=rewrite(addEventListener); It doesn't work. The code works for normal JavaScript method, for example, function hello_1(){ console.log("hello world 1!"); } hello_1=rewrite(hello_1); Need help! Thanks! 回答1: You can't count on addEventListener being a real Javascript function,

Can you implement 'sweep' using apply in R?

跟風遠走 提交于 2019-12-07 23:31:43
问题 I'm brushing up on my R skills and finally feel like I've mastered the strange sweep function e.g. df <- data.frame(a = 1:3, b = 2:4) sweep(df, MARGIN = 2, STATS = c(5, 10), FUN = "*") ## a b ## 1 5 20 ## 2 10 30 ## 3 15 40 and more usefully here, on a tutorial I'm working on implementing a spatial interaction model in R. They say that a sign you understand something is that you can say it in many ways, and I think this applies more in programming than almost anywhere else. Yet, despite the

Relative Efficiency of JOIN vs APPLY in Microsoft SQL Server 2008

故事扮演 提交于 2019-12-07 23:05:36
问题 We're just starting to look at moving to SQL 2008 from SQL 2000 and are noting the new CROSS APPLY and INNER APPLY syntax that allows a form of 'joining' against either table-valued parametrized UDFs or correlated subqueries. Obviously it would be nice to be able to encapsulate logic in a UDF and be able to reuse it in different queries, but I'm sure that functionality comes with a cost. I've looked around on the Net quite a bit but I can't find any performance metrics that indicate how much

Common Lisp 高阶函数学习笔记: function, funcall 和 apply...

孤街醉人 提交于 2019-12-07 21:49:58
Common Lisp 高阶函数学习笔记: function, funcall 和 apply 的用法 目录 0 概述 1 函数 function 的用法 2 函数 funcall 的用法 3 函数 apply 的用法 0 概述 高阶函数是 Lisp 的一大特色, 所谓的高阶函数就是把一个函数当做另一个函数的参数来用, 如果把普通的函数调用看做是在二维平面上的活动, 那么高阶函数就相当于增加了一个维度:可以把高阶函数看做在三维立体空间的活动. 一般来说, 编程语言需要这种机制, 因为这样可以为开发者提供更灵活更高级的结构抽象能力, 正如<实用 Common Lisp 编程>中所说: 在 C 语言中使用函数指针, Perl 使用子例程引用, Python 跟 Lisp 一样, C# 使用代理, Java 则使用反射和匿名类. 高阶函数的一个应用场景是通用排序, 比如 Lisp 的函数 sort , 调用形式如下: (sort '(6 2 5 3 7 0) #'>) 我们可以选择不同的比较函数(就是 #' 后面的 >), 这样的实现就比较灵活, 当我们想换一个比较函数, 如换成 < , 我们并不需要修改 sort 的代码, 只要新写一个 < 函数, 然后把它作为 sort 的参数传递进去就可以了. 另外回调函数和钩子也能够保存代码引用以便于以后运行. 1 函数 function 的用法

r dplyr - read list of files and use filename as a variable

一笑奈何 提交于 2019-12-07 18:59:36
问题 I would like to replace the "Text" below in image_annotate by the name of each file. library(dplyr) library(purrr) library(magick) list.files(path = "", pattern = "*.png", full.names = T) %>% map(image_read) %>% lapply(. %>% image_annotate("Text")) %>% image_join() %>% image_animate(fps=1) %>% image_write("animated.gif") 回答1: I think that this should work. Since you get the information about the filename and pass it to the image_read but want to use this information also for another function

find the column with lowest value in r

一个人想着一个人 提交于 2019-12-07 12:27:39
问题 I have large size matrix and try to find the column that has the minimum value for each row. For instance, here is my matrix, (simply generate with matrix(sample(12),nrow = 3) ). With the matrix I want to to have a vector (3,4,1) representing the column number which contains the lowest value in each row. How should I do it? It could be duplicated question but I could not find answers. [,1] [,2] [,3] [,4] [1,] 10 11 1 12 [2,] 8 9 7 3 [3,] 2 5 6 4 回答1: Use max.col : max.col(-mat) # [1] 3 4 1 来源

Use function instead of for loop in R: substracting previous rows iteratively with exceptions

早过忘川 提交于 2019-12-07 12:01:23
I have a large dataset for which I want to get the value of each row minus the following row except for each fifth row. With a for loop, it is fairly simple but with my large dataset, it takes over an hour. I've been told that apply with a function is MUCH faster, but I don't know how to write a complicated function and I can't find examples of similar problems. #set up matrix x=matrix(0,15,2) x[,1]=c(1, 5, 4, 3, 4, 2, 4, 3, 7, 8, 3, 2, 9, 7, 3) #run for loop for (i in c(0:((nrow(x)/5)-1)*5)){ x[i+1,2]<-x[i+1,1]-x[i+2,1] x[i+2,2]<-x[i+2,1]-x[i+3,1] x[i+3,2]<-x[i+3,1]-x[i+4,1] x[i+4,2]<-x[i+4,1

rbind matrices from a list of a list of matrices

梦想的初衷 提交于 2019-12-07 11:14:29
问题 I have a list of a list of matrices. Each list has the same number of matrices where each matrix has the same number of columns: set.seed(1) mat.lol <- list(list1=list(matrix(rnorm(100),ncol=10),matrix(rnorm(200),ncol=10),matrix(rnorm(140),ncol=10)), list2=list(matrix(rnorm(80),ncol=10),matrix(rnorm(220),ncol=10),matrix(rnorm(110),ncol=10)), list3=list(matrix(rnorm(300),ncol=10),matrix(rnorm(500),ncol=10),matrix(rnorm(650),ncol=10))) And I'd like to rbind each matrix i across all lists so

Running 'prop.test' multiple times in R

Deadly 提交于 2019-12-07 10:53:44
问题 I have some data showing a long list of regions, the population of each region and the number of people in each region with a certain disease. I'm trying to show the confidence intervals for each proportion (but I'm not testing whether the proportions are statistically different). One approach is to manually calculate the standard errors and confidence intervals but I'd like to use a built-in tool like prop.test, because it has some useful options. However, when I use prop.test with vectors,