apply

apply() in R with user-defined function

北战南征 提交于 2019-12-10 17:49:09
问题 I have a data frame with votes and party labels arranged thus dat <- data.frame( v1=c(25, 0, 70), v2=c(75, 100, 20), v3=c(0, 0, 10), l1=c("pA", ".", "pB"), l2=c("pB", "pC", "pC"), l3=c(".", ".", "pD") ) so that each row is a unit of analysis. Only vote-getting parties need consideration and this function extracts positive votes or the corresponding labels getpos <- function(vector, vorl="v"){ # change to "l" to report labels vot <- vector[grep( "v", colnames(vector) )]; lab <- vector[grep( "l

Scala apply method call as parentheses conflicts with implicit parameters

瘦欲@ 提交于 2019-12-10 16:48:41
问题 There is a note in Cay Horstmann's book "Scala for the Impatient" about the apply method: Occasionally, the () notation conflicts with another Scala feature: implicit parameters. For example, the expression "Bonjour".sorted(3) yields an error because the sorted method can optionally be called with an ordering, but 3 is not a valid ordering. The solution is to assign "Bonjour".sorted to a variable and call apply on it, for example: val result = "Bonjour".sorted result(3) Or call apply

R: t test over multiple columns using t.test function

五迷三道 提交于 2019-12-10 15:33:03
问题 I tried to perform independent t-test for many columns of a dataframe. For example, i created a data frame set seed(333) a <- rnorm(20, 10, 1) b <- rnorm(20, 15, 2) c <- rnorm(20, 20, 3) grp <- rep(c('m', 'y'),10) test_data <- data.frame(a, b, c, grp) To run the test, i used with(df, t.test(y ~ group)) with(test_data, t.test(a ~ grp)) with(test_data, t.test(b ~ grp)) with(test_data, t.test(c ~ grp)) I would like to have the outputs like this mean in group m mean in group y p-value 9.747412 9

Speedy test on R data frame to see if row values in one column are inside another column in the data frame

↘锁芯ラ 提交于 2019-12-10 15:27:39
问题 I have a data frame of marketing data with 22k records and 6 columns, 2 of which are of interest. Variable FO.variable Here's a link with the dput output of a sample of the dataframe: http://dpaste.com/2SJ6DPX Please let me know if there's a better way of sharing this data. All I want to do is create an additional binary keep column which should be: 1 if FO.variable is inside Variable 0 if FO.Variable is not inside Variable Seems like a simple thing...in Excel I would just add another column

apply function in JavaScript

拟墨画扇 提交于 2019-12-10 14:52:03
问题 I'm learning JavaScript and I'm currently trying to figure out why (in Spidermonkey) [].concat.apply([1], [[2]]) returns the expected [1, 2] , but Array.concat.apply([1], [[2]]) returns [2] instead of [1, 2] . Could someone provide a good explanation? 回答1: [].concat is Array.prototype.concat . Array.concat is a Firefox-only static method that concatenates one or more arrays and ignores its this argument. 来源: https://stackoverflow.com/questions/8669527/apply-function-in-javascript

How to assign within apply family?

狂风中的少年 提交于 2019-12-10 14:19:22
问题 I have data.frame that contains several factors and i want to rename factor levels for all of these factors. E.g.: mydf <- data.frame(col1 = as.factor(c("A","A",NA,NA)),col2 = as.factor(c("A",NA,NA,"A"))) mydf <- as.data.frame(lapply(mydf,addNA)) Note that the real life example has way more than just two columns. Hence I would like to use apply to assign other level names to all of these columns, just like in: levels(mydf$col1) <- c("1","0") I tried the following but it did not work… apply

How do I use “implicit” as apply() parameter?

╄→尐↘猪︶ㄣ 提交于 2019-12-10 13:45:18
问题 I want to do this: abstract class Context { def getInt(id: Int): Int } abstract class Dependency[+T] (val name: String, val id: Int) extends Function1[Context,T] class IntDependency(name: String, id: Int) extends Dependency[Int](name, id) { def apply(implicit context: Context): Int = context.getInt(id) } But then I get an error message like this: class IntDependency needs to be abstract, since method apply in trait Function1 of type (v1: Context)Long is not defined (Note that T1 does not

select rows in one data frame that partially match rows in another data frame

夙愿已清 提交于 2019-12-10 11:14:18
问题 I wish to select rows in one data frame, data.1 , that partially match rows in a second data frame, keep.these , to obtain the desired.result . I have found several questions here that match based on one column, but I want to match on three columns: STATE , COUNTY and CITY . I have come up with three solutions so far, but none seem ideal. Note that each row contains a unique combination of STATE , COUNTY and CITY in my real data. When I use merge I must re- order . The function match seems to

Error with xts::apply: “Error in coredata.xts(x) : currently unsupported data type”

社会主义新天地 提交于 2019-12-10 10:09:48
问题 The error occurred to me When I was trying to do the following work: # generate random integrals # data <- xts(floor(runif(100, 1,101)),as.Date("1973-02-01") + c(1:100) - 1) apply.monthly(data, diff,1,1) , while this one works: apply.monthly(data,mean) I have checked similar questions posted, but it seems they do not apply to the situation here. Any advice? Some further explanation: The reason I need this is that I got a time series data set like the following, 1990-05 100 1990-04 80 1990-03

Elastic Search : General and conditional filters

半世苍凉 提交于 2019-12-10 09:50:59
问题 I'm using Elastic Search, with query match_all and filtering. In my situation I want to apply a general filter and filters by condition. Here in pseudo: query: match all (works fine) filter range date between d1 and d2 (works fine without bullet 3) filter (apply only if field exists, but how?) etc. See the following code. I want only apply the "groups" filter if the "groups" field exists! The "exists" filter doesn't take effect in that case. "query": { "filtered": { "query": { "match_all": {}