apply

How to paste a string on each element of a vector of strings using apply in R?

余生颓废 提交于 2019-11-27 12:17:20
I have a vector of strings. d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun") for which I want to paste the string "day" on each element of the vector in a way similar to this. week <- apply(d, "day", paste, sep='') No need for apply() , just use paste() : R> d <- c("Mon","Tues","Wednes","Thurs","Fri","Satur","Sun") R> week <- paste(d, "day", sep="") R> week [1] "Monday" "Tuesday" "Wednesday" "Thursday" [4] "Friday" "Saturday" "Sunday" R> Other have already indicated that since paste is vectorised, there is no need to use apply in this case. However, to answer your question: apply is

Java collection/map apply method equivalent?

天涯浪子 提交于 2019-11-27 11:37:41
问题 I would like to apply a function to a Java collection, in this particular case a map. Is there a nice way to do this? I have a map and would like to just run trim() on all the values in the map and have the map reflect the updates. 回答1: With Java 8's lambdas, this is a one liner: map.replaceAll((k, v) -> v.trim()); For the sake of history, here's a version without lambdas: public void trimValues(Map<?, String> map) { for (Map.Entry<?, String> e : map.entrySet()) { String val = e.getValue();

How to use pandas to find consecutive same data in time series

∥☆過路亽.° 提交于 2019-11-27 10:23:23
问题 Here is a time series data like this,call it df: 'No' 'Date' 'Value' 0 600000 1999-11-10 1 1 600000 1999-11-11 1 2 600000 1999-11-12 1 3 600000 1999-11-15 1 4 600000 1999-11-16 1 5 600000 1999-11-17 1 6 600000 1999-11-18 0 7 600000 1999-11-19 1 8 600000 1999-11-22 1 9 600000 1999-11-23 1 10 600000 1999-11-24 1 11 600000 1999-11-25 0 12 600001 1999-11-26 1 13 600001 1999-11-29 1 14 600001 1999-11-30 0 I want to get the date range of the consecutive 'Value' of 1, so how can I get the final

How do I wrap a function in Javascript?

本小妞迷上赌 提交于 2019-11-27 09:32:30
问题 I'm writing a global error handling "module" for one of my applications. One of the features I want to have is to be able to easily wrap a function with a try{} catch{} block, so that all calls to that function will automatically have the error handling code that'll call my global logging method. (To avoid polluting the code everywhere with try/catch blocks). This is, however, slightly beyond my understanding of the low-level functioning of JavaScript, the .call and .apply methods, and the

apply() not working when checking column class in a data.frame

女生的网名这么多〃 提交于 2019-11-27 07:51:03
问题 I have a dataframe. I want to inspect the class of each column. x1 = rep(1:4, times=5) x2 = factor(rep(letters[1:4], times=5)) xdat = data.frame(x1, x2) > class(xdat) [1] "data.frame" > class(xdat$x1) [1] "integer" > class(xdat$x2) [1] "factor" However, imagine that I have many columns and therefore need to use apply() to help me do the trick. But it's not working. apply(xdat, 2, class) x1 x2 "character" "character" Why cannot I use apply() to see the data type of each column? or What I

Why does as.factor return a character when used inside apply?

六眼飞鱼酱① 提交于 2019-11-27 07:29:41
I want to convert variables into factors using apply() : a <- data.frame(x1 = rnorm(100), x2 = sample(c("a","b"), 100, replace = T), x3 = factor(c(rep("a",50) , rep("b",50)))) a2 <- apply(a, 2,as.factor) apply(a2, 2,class) results in: x1 x2 x3 "character" "character" "character" I don't understand why this results in character vectors instead of factor vectors. Marek apply converts your data.frame to a character matrix. Use lapply : lapply(a, class) # $x1 # [1] "numeric" # $x2 # [1] "factor" # $x3 # [1] "factor" In second command apply converts result to character matrix, using lapply : a2 <-

use multiple columns as variables with sapply

假如想象 提交于 2019-11-27 07:26:15
I have a dataframe and I would like to apply a function that takes the values of three columns and computes the minimum difference between the three values. #dataset df <- data.frame(a= sample(1:100, 10),b = sample(1:100, 10),c= sample(1:100, 10)) #function minimum_distance <- function(a,b,c) { dist1 <- abs(a-b) dist2 <- abs(a-c) dist3 <- abs(b-c) return(min(dist1,dist2,dist3)) } I am looking for something like: df$distance <- sapply(df, function(x) minimum_distance(x$a,x$b,x$c) ) ## errormessage Error in x$a : $ operator is invalid for atomic vectors While I can use ddply: df2 <- ddply(df,.(a

How does require() in node.js work?

纵然是瞬间 提交于 2019-11-27 06:39:28
I tried this: // mod.js var a = 1; this.b = 2; exports.c = 3; // test.js var mod = require('./mod.js'); console.log(mod.a); // undefined console.log(mod.b); // 2 console.log(mod.c); // 3, so this === exports? So I image that require() may be implement like this: var require = function (file) { var exports = {}; var run = function (file) { // include "file" here and run }; run.apply(exports, [file]); return exports; } Is that right? Please help me to understand require(), or where can I find the source code. Thanks! Source code is here . exports / require are not keywords, but global variables.

which list element is being processed when using snowfall::sfLapply?

旧城冷巷雨未停 提交于 2019-11-27 06:03:39
问题 Assume we have a list ( mylist ) that is use as input object for a lapply function. Is there a way to know which element in mylist is being evaluated? The method should work on lapply and snowfall::sfApply (and possible others apply family members) as well. On chat, Gavin Simpson suggested the following method. This works great for lapply but not so much for sfApply . I would like to avoid extra packages or fiddling with the list. Any suggestions? mylist <- list(a = 1:10, b = 1:10) foo <-

Why does df.apply(tuple) work but not df.apply(list)?

六月ゝ 毕业季﹏ 提交于 2019-11-27 05:28:06
Here's a dataframe: A B C 0 6 2 -5 1 2 5 2 2 10 3 1 3 -5 2 8 4 3 6 2 I could retrieve a column which is basically a tuple of columns from the original df using df.apply : out = df.apply(tuple, 1) print(out) 0 (6, 2, -5) 1 (2, 5, 2) 2 (10, 3, 1) 3 (-5, 2, 8) 4 (3, 6, 2) dtype: object But if I want a list of values instead of a tuple of them, I can't do it, because it doesn't give me what I expect: out = df.apply(list, 1) print(out) A B C 0 6 2 -5 1 2 5 2 2 10 3 1 3 -5 2 8 4 3 6 2 Instead, I need to do: out = pd.Series(df.values.tolist()) print(out) 0 [6, 2, -5] 1 [2, 5, 2] 2 [10, 3, 1] 3 [-5, 2