apply

Working with unique values at scale (for loops, apply, or plyr)

你。 提交于 2019-12-07 10:39:16
问题 I'm not sure if this is possible, but if it is, it would make life oh so much more efficient. The general problem that would be interesting to the wider SO community: for loops (and base functions like apply) are applicable for general/consistent operations, like adding X to every column or row of a data frame. I have a general/consistent operation I want to carry out, but with unique values for each element of the data frame. Is there a way to do this more efficiently than subsetting my data

Tabulating multiple response questions

我们两清 提交于 2019-12-07 08:12:27
问题 Imagine that I have a question for which there are four options, and a respondent can select zero or any combination of the four. The variables are named A , B , C , and D and the responses are stored in a data.frame as below. set.seed(1) dat = data.frame(A = sample(c(0, 1), 20, replace=TRUE), B = sample(c(0, 1), 20, replace=TRUE), C = sample(c(0, 1), 20, replace=TRUE), D = sample(c(0, 1), 20, replace=TRUE)) I can tabulate the combination of responses (for example, how many responded with A

R -apply- convert many columns from numeric to factor

穿精又带淫゛_ 提交于 2019-12-07 06:11:10
问题 I need to convert many columns that are numeric to factor type. An example table: df <- data.frame(A=1:10, B=2:11, C=3:12) I tried with apply: cols<-c('A', 'B') df[,cols]<-apply(df[,cols], 2, function(x){ as.factor(x)}); But the result is a character class. > class(df$A) [1] "character" How can I do this without doing as.factor for each column? 回答1: Try df[,cols] <- lapply(df[,cols],as.factor) The problem is that apply() tries to bind the results into a matrix, which results in coercing the

Rewriting loops with apply functions

为君一笑 提交于 2019-12-07 05:46:13
问题 I have the 3 following functions which I would like to make faster, I assume apply functions are the best way to go, but I have never used apply functions, so I have no idea what to do. Any type of hints, ideas and code snippets will be much appreciated. n, T, dt are global parameters and par is a vector of parameters. Function 1: is a function to create an m+1,n matrix containing poisson distributed jumps with exponentially distributed jump sizes. My troubles here is because I have 3 loops

using data.table to speed up rollapply

时光总嘲笑我的痴心妄想 提交于 2019-12-07 03:46:04
问题 I am new to data.tables so apologies if this is a very basic question. I have heard that data.tables significantly improves computational times when working with large amounts data, and so would like to see if data.table is able to help in speeding up the rollapply function. if we have some univariate data xts.obj <- xts(rnorm(1e6), order.by=as.POSIXct(Sys.time()-1e6:1), tz="GMT") colnames(xts.obj) <- "rtns" a simple rolling quantile with width of 100 and a p of 0.75 takes a surprisingly long

Apply function over two vectors of different lengths, and return a matrix in R

会有一股神秘感。 提交于 2019-12-07 02:51:51
问题 I have two vectors of different lengths, and I would like to apply a function to every possible combination of the two vectors, resulting in a matrix. In my particular example, the two vectors are charactor vectors, and I would like to apply the function grepl , ie: names <- c('cats', 'dogs', 'frogs', 'bats') slices <- c('ca', 'at', 'ts', 'do', 'og', 'gs', 'fr', 'ro', 'ba') results <- someFunction(grepl, names, slices) results ca at ts do og gs fr ro ba cats TRUE TRUE TRUE FALSE FALSE FALSE

Using Apply family of functions on mts objects

核能气质少年 提交于 2019-12-07 00:34:20
问题 Using apply (or sapply) on an mts object removes its time series properties when sending to function. How should I apply same function (with ts input and ts output) on each of times series in an mts object and return it (preferably as mts) [I mean besides using for loops]? For example suppose I write a function that returns the trend of a time series (using stl) myfunc <- function(x) { return(stl(x,"per")$time.series[,2]) } Now for a sample mts z <- ts(matrix(rnorm(90), 30, 3), start=c(1961,

R sapply is.factor

夙愿已清 提交于 2019-12-07 00:28:06
问题 I'm trying to separate a dataset into parts that have factor variables and non-factor variables. I'm looking to do something like: This part works: factorCols <- sapply(df1, is.factor) factorDf <- df1[,factorCols] This part won't work: nonFactorCols <- sapply(df1, !is.factor) due to this error: Error in !is.factor : invalid argument type Is there a correct way to do this? 回答1: Correct way: nonFactorCols <- sapply(df1, function(col) !is.factor(col)) # or, more efficiently nonFactorCols <-

kotlin how to refer outer-scope this in multi-layer apply functions

北城余情 提交于 2019-12-06 21:52:12
问题 for example: v1?.apply { v2?.apply { call(this, target, outerThis); } } my question is how to refer to "outerThis"? thanks for any help. 回答1: You can use a label and then a qualified this expression: v1?.apply outer@ { v2?.apply { call(this, target, this@outer) } } 回答2: It's generally not recommended to use nested apply calls, which is to avoid your situation. You may of course use labels as a workaround, but you may also use also as an alternative: v1?.also { outer -> v2?.apply { call(this,

Lisp-Stat 翻译 —— 第三章 Lisp编程

拈花ヽ惹草 提交于 2019-12-06 21:24:20
第三章 Lisp编程 上一章我们使用了一些内建的Lisp函数和Lisp-Stat函数来运行一些有趣的运算。我们构建的表达式中的一些还是相当复杂的。当你发觉自己多次键入相同的表达式的时候(当然你使用的数据可能略微有些不同),你自然就想为这个表达式引入一些速记符,也就是说你想要定义自己的函数了。函数定义是Lisp编程的基础操作。第2.7节已经对这个主题给出了一个简单的介绍,现在是深入探索的时候了。在对如何定义一个Lisp函数进行一个简略的复习之后,我们将检验一些需要的技术以开发功能更强大的函数:状态求值、递归和迭代、局部变量、函数式数据、映射和赋值。 除了介绍定义函数的工具,本章也展示了一些Lisp编程常用编程技术和原则。尤其地,对于本章的大多数情况我都会使用函数式风格直至本章结束,目的是避免使用赋值方式改变变量的值(注:这里提到的原则也是函数式的目的之一,即函数可以使用外部变量,但在函数的整个执行过程中不对变量进行写操作,不破坏外部变量,这样的函数要非破坏性函数,反之叫破坏性函数,在《Practical Common Lisp》和《On Lisp》里你将接触大量的非破坏性函数和他们的破坏性版本,都有其各自的书写约定)。本章的开发严重依赖Abelson和Sussman的《 Scheme:计算机程序结构与解释》的 前两章。为了允许我们集中精力到编程过程本身