apply

Python - Iterate over a list of attributes

孤人 提交于 2019-12-02 03:30:39
问题 I have a feature in my data set that is a pandas timestamp object. It has (among many others) the following attributes: year, hour, dayofweek, month. I can create new features based on these attributes using some brute force methods: df["year"] = df["timeStamp"].apply(lambda x : x.year) df["hour"] = df["timeStamp"].apply(lambda x : x.hour) . . . However, I want to iterate over a list: nomtimes = ["year", "hour", "month", "dayofweek"] for i in nomtimes: df[i] = df["timeStamp"].apply(lambda x :

Behavior of identical() in apply in R

。_饼干妹妹 提交于 2019-12-02 02:27:58
问题 This is weird. apply( matrix(c(1,NA,2,3,NA,NA,2,4),ncol = 2), 1, function(x) identical(x[1], x[2]) ) #[1] FALSE TRUE TRUE FALSE apply( data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4)), 1, function(x) identical(x[1], x[2]) ) #[1] FALSE FALSE FALSE FALSE apply( as.matrix(data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4))), 1, function(x) identical(x[1], x[2]) ) #[1] FALSE FALSE FALSE FALSE This is due to the names attribute as indicated below by joran. I can obtain the result I expected by: apply( data

Behavior of identical() in apply in R

亡梦爱人 提交于 2019-12-02 02:22:13
This is weird. apply( matrix(c(1,NA,2,3,NA,NA,2,4),ncol = 2), 1, function(x) identical(x[1], x[2]) ) #[1] FALSE TRUE TRUE FALSE apply( data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4)), 1, function(x) identical(x[1], x[2]) ) #[1] FALSE FALSE FALSE FALSE apply( as.matrix(data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4))), 1, function(x) identical(x[1], x[2]) ) #[1] FALSE FALSE FALSE FALSE This is due to the names attribute as indicated below by joran. I can obtain the result I expected by: apply( data.frame(a = c(1,NA,2,3),b = c(NA,NA,2,4)), 1, function(x) identical(unname(x[1]), unname(x[2])) ) or:

Does it make sense to use .apply( ) and pass the same instance as context?

。_饼干妹妹 提交于 2019-12-02 02:10:06
I'm reading Javascript Web Applications, from O'Reilly. At various points in the book, the author uses something along the following: instance.init.apply(instance, arguments); Does this make any sense? Isn't this exactly the same as: instance.init(arguments); .call() and .apply() are used to manually set the execution context of a function. Why should I use them when I'm intending to use the original execution context anyway? The point is that arguments is an array-like object. Doing ... instance.init(arguments); ... passes one argument, which is an array-like object containing certain

Python - Iterate over a list of attributes

妖精的绣舞 提交于 2019-12-02 00:03:12
I have a feature in my data set that is a pandas timestamp object. It has (among many others) the following attributes: year, hour, dayofweek, month. I can create new features based on these attributes using some brute force methods: df["year"] = df["timeStamp"].apply(lambda x : x.year) df["hour"] = df["timeStamp"].apply(lambda x : x.hour) . . . However, I want to iterate over a list: nomtimes = ["year", "hour", "month", "dayofweek"] for i in nomtimes: df[i] = df["timeStamp"].apply(lambda x : x.i) I get the following AttributeError: 'Timestamp' object has no attribute 'i', and I get it and

Find a percentage based on multiple columns of criteria in R

南楼画角 提交于 2019-12-01 23:27:34
I have multiple columns and I would like to find the percentage of a one column in the other columns are the same. For example; ST cd variable 1 1 23432 1 1 2345 1 2 908890 1 2 350435 1 2 2343432 2 1 9999 2 1 23432 so what I'd like to do is: if ST and cd are the same, then find the percentage of variable for that row over all with the same ST and cd. So in the end it would look like: ST cd variable percentage 1 1 23432 90.90% 1 1 2345 9.10% 1 2 908890 25.30% 1 2 350435 9.48% 1 2 2343432 65.23% 2 1 9999 29.91% 2 1 23432 70.09% How can I do this in R? Thanks for all the help. You can create your

Replacing matrix elements indexed by another matrix

余生颓废 提交于 2019-12-01 23:11:54
After several hours of searching, I am turning to your expertise. Beginner in R, I try to speed up my code. My goal is to replace the values in a matrix A . However, I want to replace values based on two vectors of another matrix B . B[, 1] is the name of row i of the matrix A . The second column, B[, 2] corresponds to the name of column of the matrix A . The first version of my code was to use the match function in a loop. for(k in 1:L){ i <- B[k,1] j <- B[k,2] d <- match(i,rownames(A)) e <- match(j,colnames(A)) A[d, e] <- 0 } The second version allowed me to speed a little bit: for( k in 1:L

mapply over two lists [closed]

两盒软妹~` 提交于 2019-12-01 21:13:23
I recentely asked a question about using an apply function over two lists. Each list is a list of data frames created by splitting a large dataframe. For each time the function runs I want to take vectors from the first element (a dataframe) in mylist1 and some vectors from the first element (a dataframe) in mylist2 and regress them against each other. Then move onto the next mylist1 element and mylist2 element. Effectively the function takes two lists with the same number of elements and takes a pair (one from each list) and plays about with them. I tried the following, but the results I get

Anova, for loop to apply function

大城市里の小女人 提交于 2019-12-01 20:14:07
>str(set) 'data.frame': 1000 obs. of 6 variables: $ ID : Factor .. $ a : Factor .. $ b: Factor .. $ c: Factor .. $ dat : num .. $ contrasts : Ord.factor .. >X [1] "a" "b" "c" for (i in 1 :length(X) ){ my=X[i] f=as.formula(paste("dat~contrasts*", paste(my,"Error(ID/(contrasts))",sep="+"))) sum = summary( aov (f, data =set)) } X can be very huge, so was thinking about an apply function instead of for-loop.Is it possible in this case?? I tried this: apply( as.matrix(X), 1, function(i){ summary(aov(as.formula(paste("dat~contrasts*", paste(i, "Error(ID/(contrasts))", sep="+"))), data=set)) } ) But

How to bind function arguments

旧巷老猫 提交于 2019-12-01 20:04:21
How do I partially bind/apply arguments to a function in R? This is how far I got, then I realized that this approach doesn't work... bind <- function(fun,...) { argNames <- names(formals(fun)) bindedArgs <- list(...) bindedNames <- names(bindedArgs) function(argNames[!argNames %in% bindedArgs]) { #TODO } } Thanks! Have you tried looking at roxygen's Curry function? > library(roxygen) > Curry function (FUN, ...) { .orig = list(...) function(...) do.call(FUN, c(.orig, list(...))) } <environment: namespace:roxygen> Example usage: > aplusb <- function(a,b) { + a + 2*b + } > oneplusb <- Curry