apply

`tapply()` to return data frame

陌路散爱 提交于 2019-12-21 21:27:17
问题 I have a dataset with a datetime (POSIXct), a "node" (factor) and and a "c" (numeric) columns, for example: date node c 1 2011-08-14 10:30:00 2 0.051236000 2 2011-08-14 10:30:00 2 0.081230000 3 2011-08-14 10:31:00 1 0.000000000 4 2011-08-14 10:31:00 4 0.001356337 5 2011-08-14 10:31:00 3 0.001356337 6 2011-08-14 10:32:00 2 0.000000000 I need to take the mean of column "c" for all pairs of "date" and "node", so I did this: tapply(data$c, list(data$node, data$date), mean) The result I obtain is

How to apply rolling functions in a group by object in pandas

China☆狼群 提交于 2019-12-21 12:59:29
问题 I'm having difficulty to solve a look-back or roll-over problem in dataframe or perhaps in groupby. The following is a simple example of the dataframe I have: fruit amount 20140101 apple 3 20140102 apple 5 20140102 orange 10 20140104 banana 2 20140104 apple 10 20140104 orange 4 20140105 orange 6 20140105 grape 1 … 20141231 apple 3 20141231 grape 2 I need to calculate the average value of 'amount' of each fruit in the previous 3 days for everyday, and create the following data frame: fruit

How to apply rolling functions in a group by object in pandas

旧巷老猫 提交于 2019-12-21 12:59:26
问题 I'm having difficulty to solve a look-back or roll-over problem in dataframe or perhaps in groupby. The following is a simple example of the dataframe I have: fruit amount 20140101 apple 3 20140102 apple 5 20140102 orange 10 20140104 banana 2 20140104 apple 10 20140104 orange 4 20140105 orange 6 20140105 grape 1 … 20141231 apple 3 20141231 grape 2 I need to calculate the average value of 'amount' of each fruit in the previous 3 days for everyday, and create the following data frame: fruit

Pandas transform() vs apply()

我与影子孤独终老i 提交于 2019-12-21 07:26:28
问题 I don't understand why apply and transform return different dtypes when called on the same data frame. The way I explained the two functions to myself before went something along the lines of " apply collapses the data, and transform does exactly the same thing as apply but preserves the original index and doesn't collapse." Consider the following. df = pd.DataFrame({'id': [1,1,1,2,2,2,2,3,3,4], 'cat': [1,1,0,0,1,0,0,0,0,1]}) Let's identify those id s which have a nonzero entry in the cat

How to use apply, cat and print, without getting NULL

我与影子孤独终老i 提交于 2019-12-21 04:52:29
问题 I am trying to use cat() as functions inside apply(). I can almost make R do what I want, but I'm getting some very confusing (to me) NULLS at the end of the return. Here is a silly example, to highlight what I'm getting. val1 <- 1:10 val2 <- 25:34 values <- data.frame(val1, val2) apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE)) This "works" in that R accepts it and it runs, but I don't understand the results. > apply(values, 1, function(x) cat(x[1], x[2], fill=TRUE)) 1 25 2 26 3 27 4

R return the index of the minimum column for each row

孤街浪徒 提交于 2019-12-21 03:48:28
问题 I have a data.frame that contains 4 columns (given below). I want to find the index of the minimum column (NOT THE VALUE) for each row. Any idea hiw to achieve that? > d V1 V2 V3 V4 1 0.388116155 0.98999967 0.41548536 0.76093748 2 0.495971331 0.47173142 0.51582728 0.06789924 3 0.436495321 0.48699268 0.21187838 0.54139290 4 0.313514389 0.50265539 0.08054103 0.46019601 5 0.277275961 0.39055360 0.29594162 0.70622532 6 0.264804739 0.86996266 0.85708635 0.61136741 7 0.627344463 0.54277873 0

How to put an apply equivalent to any for loop

自作多情 提交于 2019-12-21 02:03:32
问题 Most pro R users have advised me never to use loops in R. Use apply functions instead. The problem is that it is not that intuitive to write an apply equivalent for every for/while loop if you're not familiar with functional programming. Take the below example for instance. F <- data.frame(name = c("a", "b", "c", "d"), var1 = c(1,0,0,1), var2 = c(0,0,1,1), var3 = c(1,1,1,1), clus = c("one", "two", "three", "four")) F$ObjTrim <- "" for (i in 1:nrow(F)) { for (j in 2:(ncol(F)-1)) { if(F[i, j] =

Apply a function to all the elements of a data frame

给你一囗甜甜゛ 提交于 2019-12-20 09:38:01
问题 I am trying to apply some transformations to all the elements in a dataframe. When using the regular apply functions, I get a matrix back and not a dataframe. Is there a way to get a dataframe directly without adding as.data.frame to each line? df = data.frame(a = LETTERS[1:5], b = LETTERS[6:10]) apply(df, 1, tolower) #Matrix apply(df, 2, tolower) #Matrix sapply(df, tolower) #Matrix as.data.frame(sapply(df, tolower)) # Can I avoid "as.data.frame"? 回答1: We can use lapply and assign it back to

How to calculate Mean by Date Grouped as Fiscal Quarters

半腔热情 提交于 2019-12-20 07:29:16
问题 I have the following table: Date Country Class Value 6/1/2010 USA A 45 6/1/2010 Canada A 23 6/1/2010 Brazil B 65 9/1/2010 USA B 47 9/1/2010 Canada A 98 9/1/2010 Brazil B 25 12/1/2010 USA B 14 12/1/2010 Canada A 79 12/1/2010 Brazil A 23 3/1/2011 USA A 84 3/1/2011 Canada B 77 3/1/2011 Brazil A 43 6/1/2011 USA A 45 6/1/2011 Canada A 23 6/1/2011 Brazil B 65 9/1/2011 USA B 47 9/1/2011 Canada A 98 9/1/2011 Brazil B 25 12/1/2011 USA B 14 12/1/2011 Canada A 79 12/1/2011 Brazil A 23 3/1/2012 USA A 84

Aggregate sum obs with different ID's in the same data frame

拟墨画扇 提交于 2019-12-20 06:14:24
问题 My goal is to make another column by summing the observation from the present day and all previous observations from the same ID by using the date (the data set is sorted in date and chr nr(ID). I will need the aggregation to start over when a new "id" is presented. there might be som NA's, they should be considered as null "Doseringer_pr_kg_dyr" is the observation. CHR_NR DATO_AFSLUT Doseringer_pr_kg_dyr brugstid 10358 2018-08-06 29416.67 31 10358 2018-09-06 104682.27 36 10358 2018-10-12