apply

How to switch rows in R?

三世轮回 提交于 2019-12-11 13:19:48
问题 I have a array with following content: > head(MEAN) 1901DJF 1901JJA 1901MAM 1901SON 1902DJF 1902JJA -0.45451556 -0.72922229 -0.17669396 -1.12095590 -0.86523850 -0.04031273 This should be a time series with seasonal mean values from 1901 to 2009. The problem is that the generated column heads are strictly alphabetically ordered. However, in terms of season this doesn't make to much sense, e.g. JJA (june, july, august) is leading MAM (march, april, may). How could I switch each MAM and JJA

Rollapply() backwards in R

别等时光非礼了梦想. 提交于 2019-12-11 12:15:39
问题 Is there a way to apply rollapply() backwards say from 100 to 1 instead of 1 to 100 or should I sort my data first and then apply rollaplly()? 回答1: Try this z = 1:100 #Normal rollapply rollapply(z, 2, mean) #Reverese rollapply(z[length(z):1], 2, mean) 来源: https://stackoverflow.com/questions/24056809/rollapply-backwards-in-r

Using groupby and loc to set up a new dataframe

╄→гoц情女王★ 提交于 2019-12-11 12:05:14
问题 Hi I have a data frame as follow: df = pd.DataFrame() df['Team1'] = ['A','B','C','D','E','F','A','B','C','D','E','F'] df['Score1'] = [1,2,3,1,2,4,1,2,3,1,2,4] df['Team2'] = ['U','V','W','X','Y','Z','U','V','W','X','Y','Z'] df['Score2'] = [2,1,2,2,3,3,2,1,2,2,3,3] df['Match'] = df['Team1'] + ' Vs '+ df['Team2'] df['Match_no']= [1,2,3,4,5,6,1,2,3,4,5,6] df['model'] = ['ELO','ELO','ELO','ELO','ELO','ELO','xG','xG','xG','xG','xG','xG'] winner = df.Score1>df.Score2 df['winner'] = np.where(winner

fit model to multiple groupings or subsets and extract original factor columns for data frame output

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 11:13:53
问题 I want to fit models and pull out specific parameters split by grouping factors (fac1 and fac2 below) or subsets. My problem is that when sapply outputs the correct parameters, I'm stuck with a list where the elements are named as combinations. What I want to get is a data.frame where I have a column for each factor with the appropriate label. I want to do this in base R . Notice, the answer needs to be general and not for the specific names used in this case. The answer shouldn't be hindered

Apply family of functions for functions with multiple arguments

限于喜欢 提交于 2019-12-11 10:25:16
问题 I would like to use a function from the apply family (in R) to apply a function of two arguments to two matrices. I assume this is possible. Am I correct? Otherwise, it would seem that I have to put the two matrices into one, and redefine my function in terms of the new matrix. Here's an example of what I'd like to do: a <- matrix(1:6,nrow = 3,ncol = 2) b <- matrix(7:12,nrow = 3,ncol = 2) foo <- function(vec1,vec2){ d <- sample(vec1,1) f <- sample(vec2,1) result <- c(d,f) return(result) } I

apply regexp in one data frame based on the column in another data frame

China☆狼群 提交于 2019-12-11 10:17:36
问题 I have two data frames --- table A is the pattern table, and table B is the name table. I want to subset table B, where it matches the pattern in table a. A <- data.frame(pattern = c("aa", "bb", "cc", "dd")) B <- data.frame(name = "aa1", "bb1", "abc", "def" ,"ddd") I'm trying to do a for loop looks like: for (i in 1:nrow(A)){ for (j in 1:nrow(B)){ DT <- data.frame(grep(A$pattern[i], B$name[j], ignore.case = T, value = T)) }} And I want my resulting table DT to only contains aa1 , bb1 , and

apply function to two pandas dataframes in python (scipy.stats.spearmanr for each row from two dataframes)

吃可爱长大的小学妹 提交于 2019-12-11 09:01:39
问题 I have two panda dataframe: price and sales dataframe. price dataframe records price for each product (columns) in each year (index) |a |b |c |d |e | 2018|3.2|4.5|5.6|7.8|8.1| 2017|6.2|1.5|2.6|7.8|2.1| 2016|2.2|9.5|0.6|6.8|4.1| 2015|2.2|6.5|7.6|7.8|2.1| sales dataframe (see below) records sales for each product (columns) in each year (index) |a |b |c |d |e | 2018|101|405|526|108|801| 2017|601|105|726|308|201| 2016|202|965|856|408|411| 2015|322|615|167|458|211| I would like to calculate

R trailing cumsum per group

独自空忆成欢 提交于 2019-12-11 08:36:45
问题 I need to compute the running cumsum per group in R but the window over which to cumsum must only be the last 3 observations: If for example I have a table with a person's name, a date and a score as follow: Name Date Score 1 John 2017-01-01 4 2 John 2017-01-02 5 3 John 2017-01-03 3 4 John 2017-01-04 1 5 John 2017-01-05 4 6 John 2017-01-06 4 7 Ben 2017-01-01 4 8 Ben 2017-01-02 4 9 Ben 2017-01-03 5 10 Ben 2017-01-04 2 11 Ben 2017-01-05 3 12 Ben 2017-01-06 4 13 Ben 2017-01-07 4 14 Ben 2017-01

Use apply on a multi-dimension array

心不动则不痛 提交于 2019-12-11 06:46:33
问题 A normal matrix would be 2-dimension matrix. But, I can initialise: a<-array(0,dim=c(2,3,4,5)) Which is a 2*4*5*3 matrix, or array. Command apply(a,c(2,3),sum) will give a 4*5 array, contain the sum over elements in the 1st and 4th dimension. Why it that? As far as I know, in the apply function, 1 indicates rows, 2 indicates columns, but what does 3 mean here? We need some abstraction here. 回答1: The easiest way to understand apply on an array is to try some examples. Here's some data modified

Pandas overwrite values in column selectively based on condition from another column

▼魔方 西西 提交于 2019-12-11 06:34:11
问题 I have a dataframe in pandas with four columns. The data consists of strings. Sample: A B C D 0 2 asicdsada v:cVccv u 1 4 ascccaiiidncll v:cVccv:ccvc u 2 9 sca V:c u 3 11 lkss v:cv u 4 13 lcoao v:ccv u 5 14 wuduakkk V:ccvcv: u I want to replace the string 'u' in Col D with the string 'a' if Col C in that row contains the substring 'V' (case sensitive). Desired outcome: A B C D 0 2 asicdsada v:cVccv a 1 4 ascccaiiidncll v:cVccv:ccvc a 2 9 sca V:c a 3 11 lkss v:cv u 4 13 lcoao v:ccv u 5 14