apply

Why does function apply complain about long lists?

柔情痞子 提交于 2019-12-05 13:24:53
As part of some Eulerian travails , I'm trying to code a Sieve of Eratosthenes with a factorization wheel. My code so far is: (defun ring (&rest content) "Returns a circular list containing the elements in content. The returned list starts with the first element of content." (setf (cdr (last content)) content)) (defun factorization-wheel (lst) "Returns a circular list containing a factorization wheel using the list of prime numbers in lst" (let ((circumference (apply #'* lst))) (loop for i from 1 to circumference unless (some #'(lambda (x) (zerop (mod i x))) lst) collect i into wheel finally

Running 'prop.test' multiple times in R

百般思念 提交于 2019-12-05 13:16:49
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, it runs a chi-square test across all the proportions. I've solved this with a while loop (see dummy

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

天涯浪子 提交于 2019-12-05 12:24:11
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 frame for every grouping, applying the function with specific numbers relative to that grouping, then

The value is returned instead of NULL when using function with OUTER APPLY

橙三吉。 提交于 2019-12-05 11:04:50
问题 I am getting strange results when using inline function. Here is the code: IF EXISTS ( SELECT * FROM sys.objects AS o WHERE name = 'vendor_relation_users' ) DROP FUNCTION dbo.vendor_relation_users; GO CREATE FUNCTION [dbo].[vendor_relation_users] ( @user_name CHAR(12) ) RETURNS TABLE AS RETURN (SELECT @user_name AS user_name WHERE @user_name NOT LIKE '06%'); GO DECLARE @u CHAR(12) = '066BDLER' SELECT a.user_name, is_v.user_name FROM (SELECT @u AS user_name) a OUTER APPLY [dbo].[vendor

Parallelize pandas apply

那年仲夏 提交于 2019-12-05 09:09:55
New to pandas, I already want to parallelize a row-wise apply operation. So far I found Parallelize apply after pandas groupby However, that only seems to work for grouped data frames. My use case is different: I have a list of holidays and for my current row/date want to find the no-of-days before and after this day to the next holiday. This is the function I call via apply: def get_nearest_holiday(x, pivot): nearestHoliday = min(x, key=lambda x: abs(x- pivot)) difference = abs(nearesHoliday - pivot) return difference / np.timedelta64(1, 'D') How can I speed it up? edit I experimented a bit

Ignoring values or NAs in the sample function

十年热恋 提交于 2019-12-05 09:04:49
I have a matrix in R that I would like to take a single random sample from each row. Some of my data is in NA, but when taking the random sample I do not want the NA to be an option for the sampling. How would I accomplish this? For example, a <- matrix (c(rep(5, 10), rep(10, 10), rep(NA, 5)), ncol=5, nrow=5) a [,1] [,2] [,3] [,4] [,5] [1,] 5 5 10 10 NA [2,] 5 5 10 10 NA [3,] 5 5 10 10 NA [4,] 5 5 10 10 NA [5,] 5 5 10 10 NA When I apply the sample function to this matrix to output another matrix I get b <- matrix(apply(a, 1, sample, size=1), ncol=1) b [,1] [1,] NA [2,] NA [3,] 10 [4,] 10 [5,]

R -apply- convert many columns from numeric to factor

僤鯓⒐⒋嵵緔 提交于 2019-12-05 08:27:44
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? 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 columns to character: class(apply(df[,cols], 2, as.factor)) ## matrix class(as.factor(df[,1])) ## factor In

using apply with assign in R

烂漫一生 提交于 2019-12-05 06:37:29
Consider the following example: Vars <- c("car","bike","lorry") Dat <- c(10,20,22) for (i in 1:length(Vars)){ assign(Vars[i],Dat[i]) } Here, I would like to generate three variables in the workspace named according to the entries in Vars and the values in Dat . At the moment I am using a loop, but I have been trying to remove the loop by using apply, how would be the best way of doing this? This is a great example of when to use a for loop instead of an apply . The best solution is to leave it as it is. if you really want to use an *ply loop, use mapply mapply(assign, Vars, Dat, MoreArgs=list

Using Apply family of functions on mts objects

岁酱吖の 提交于 2019-12-05 05:17:10
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, 1), frequency=4) class(z) Sending only one of the time series works correct: myfunc(z[,1]) # works

R sapply is.factor

*爱你&永不变心* 提交于 2019-12-05 04:16:08
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? Correct way: nonFactorCols <- sapply(df1, function(col) !is.factor(col)) # or, more efficiently nonFactorCols <- !sapply(df1, is.factor) # or, even more efficiently nonFactorCols <- !factorCols Joshua gave you the correct way