apply

Why apply() returns a transposed xts matrix?

倾然丶 夕夏残阳落幕 提交于 2019-11-26 07:48:57
问题 I want to run a function on all periods of an xts matrix. apply() is very fast but the returned matrix has transposed dimensions compared to the original object: > dim(myxts) [1] 7429 48 > myxts.2 = apply(myxts, 1 , function(x) { return(x) }) > dim(myxts.2) [1] 48 7429 > str(myxts) An \'xts\' object from 2012-01-03 09:30:00 to 2012-01-30 16:00:00 containing: Data: num [1:7429, 1:48] 4092500 4098500 4091500 4090300 4095200 ... - attr(*, \"dimnames\")=List of 2 ..$ : NULL ..$ : chr [1:48] \

How does the Math.max.apply() work?

隐身守侯 提交于 2019-11-26 07:33:06
How does the Math.max.apply() work?. <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>JS Bin</title> </head> <body> <script> var list = ["12","23","100","34","56", "9","233"]; console.log(Math.max.apply(Math,list)); </script> </body> </html> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max The above code finds the Max number in the List. Can anyone tell me how does the below code work?. It seems it works if i pass null or Math. console.log(Math.max.apply(Math,list)); Does all the user-defined/Native functions have call and apply method which

Is there a R function that applies a function to each pair of columns?

二次信任 提交于 2019-11-26 06:38:08
问题 I often need to apply a function to each pair of columns in a dataframe/matrix and return the results in a matrix. Now I always write a loop to do this. For instance, to make a matrix containing the p-values of correlations I write: df <- data.frame(x=rnorm(100),y=rnorm(100),z=rnorm(100)) n <- ncol(df) foo <- matrix(0,n,n) for ( i in 1:n) { for (j in i:n) { foo[i,j] <- cor.test(df[,i],df[,j])$p.value } } foo[lower.tri(foo)] <- t(foo)[lower.tri(foo)] foo [,1] [,2] [,3] [1,] 0.0000000 0.7215071

AttributeError: &#39;PandasExprVisitor&#39; object has no attribute &#39;visit_Ellipsis&#39;, using pandas eval

怎甘沉沦 提交于 2019-11-26 06:08:53
问题 I have a series of the form: s 0 [133, 115, 3, 1] 1 [114, 115, 2, 3] 2 [51, 59, 1, 1] dtype: object Note that its elements are strings : s[0] \'[133, 115, 3, 1]\' I\'m trying to use pd.eval to parse this string into a column of lists. This works for this sample data. pd.eval(s) array([[133, 115, 3, 1], [114, 115, 2, 3], [51, 59, 1, 1]], dtype=object) However, on much larger data (order of 10K), this fails miserably! len(s) 300000 pd.eval(s) AttributeError: \'PandasExprVisitor\' object has no

Sorting rows alphabetically

瘦欲@ 提交于 2019-11-26 04:55:50
问题 My data looks like, A B C D B C A D X Y M Z O M L P How can I sort the rows to get something like A B C D A B C D M X Y Z L M O P Thanks, 回答1: t(apply(DF, 1, sort)) The t() function is necessary because row operations with the apply family of functions returns the results in column-major order. 回答2: What did you try? This is really straight-forward and easy to solve with a simple loop. > s <- x > for(i in 1:NROW(x)) { + s[i,] <- sort(s[i,]) + } > s V1 V2 V3 V4 1 A B C D 2 A B C D 3 M X Y Z 4

Faster way to read fixed-width files

大城市里の小女人 提交于 2019-11-26 03:55:44
问题 I work with a lot of fixed width files (i.e., no separating character) that I need to read into R. So, there is usually a definition of the column width to parse the string into variables. I can use read.fwf to read in the data without a problem. However, for large files, this can take a long time. For a recent dataset, this took 800 seconds to read in a dataset with ~500,000 rows and 143 variables. seer9 <- read.fwf(\"~/data/rawdata.txt\", widths = cols, header = FALSE, buffersize = 250000,

python pandas: apply a function with arguments to a series

旧街凉风 提交于 2019-11-26 01:42:24
问题 I want to apply a function with arguments to a series in python pandas: x = my_series.apply(my_function, more_arguments_1) y = my_series.apply(my_function, more_arguments_2) ... The documentation describes support for an apply method, but it doesn\'t accept any arguments. Is there a different method that accepts arguments? Alternatively, am I missing a simple workaround? Update (October 2017): Note that since this question was originally asked that pandas apply() has been updated to handle

pandas create new column based on values from other columns / apply a function of multiple columns, row-wise

醉酒当歌 提交于 2019-11-26 01:27:29
问题 I want to apply my custom function (it uses an if-else ladder) to these six columns ( ERI_Hispanic , ERI_AmerInd_AKNatv , ERI_Asian , ERI_Black_Afr.Amer , ERI_HI_PacIsl , ERI_White ) in each row of my dataframe. I\'ve tried different methods from other questions but still can\'t seem to find the right answer for my problem. The critical piece of this is that if the person is counted as Hispanic they can\'t be counted as anything else. Even if they have a \"1\" in another ethnicity column they

How does the Math.max.apply() work?

ε祈祈猫儿з 提交于 2019-11-26 01:14:40
问题 How does the Math.max.apply() work?. <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>JS Bin</title> </head> <body> <script> var list = [\"12\",\"23\",\"100\",\"34\",\"56\", \"9\",\"233\"]; console.log(Math.max.apply(Math,list)); </script> </body> </html> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max The above code finds the Max number in the List. Can anyone tell me how does the below code work?. It seems it works if i pass null or Math.

Why isn&#39;t my Pandas &#39;apply&#39; function referencing multiple columns working? [closed]

China☆狼群 提交于 2019-11-26 00:18:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 9 months ago . I have some problems with the Pandas apply function, when using multiple columns with the following dataframe df = DataFrame ({\'a\' : np.random.randn(6), \'b\' : [\'foo\', \'bar\'] * 3, \'c\' : np.random.randn(6)}) and the following function def my_test(a, b): return a % b When I try to apply this function