Questions tagged [tapply]

tapply is a function in the R programming language for apply a function to subsets of a vector.

tapply is a function in the R programming language for apply a function to subsets of a vector. A vector is broken in to subsets, potentially of different lengths (aka a ragged array) based on the values of one or more other vector. The second vector is either already a factor or coerced to be a factor by as.factor. A function is applied to each of these subsets. tapply then returns either an array or a list, depending on the output of the function.

354 questions
2
votes
1 answer

How to convert a dataframe into an array in R?

I am interested in calculate the medians of a data.frame. I found two approaches to do it: 1) with tapply and 2) with aggregate. For many reasons, I am more interested in the "aggregate approach". However, since it gives me a data.frame and I need…
emr2
  • 1,436
  • 7
  • 23
2
votes
3 answers

How to extract the name of a column from a data frame to be used in the loop?

I would like to copy the text of a data frame's column names one-by-one in a for loop. My code seems to return NULL values from the column name argument. More broadly, I want to create a summary by factor of each of several columns. # Create an…
bhbennett3
  • 123
  • 8
2
votes
2 answers

Compute user defined function output by group in R

I am trying to calculate z-statistic over regular interval of rows. mean = 77 std = 31 samp.45 = rnorm(45,mean,std) z.test = function(a, mu, sd){ zeta = (mean(a) - mu) / (sd / sqrt(length(a))) return(zeta) } z.hypothesis = function(a,…
Manish
  • 458
  • 6
  • 19
2
votes
1 answer

R : Percentile 90% with tapply

I am trying to apply the quantile function to a column (y) of my data (tab) considering the groups (column x): z <- with (tab, tapply (y, x, quantile)) tab       x y 1 1 0.11 2 1 0.07 3 0.04 4 2 0.39 5 2 0.12 6 3 0.21 7 3 0.06 8 3 0.00 9 3 0.12 10…
Ph.D.Student
  • 704
  • 6
  • 27
2
votes
4 answers

Summing rows of a matrix based on column index

I am trying to go from a matrix that has columns that "belong together" to one where the row-sums of the relevant sub-matrices have been formed. I.e. going from [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]…
Björn
  • 644
  • 10
  • 23
2
votes
1 answer

How to use aggregate( ) to count NA values and using tapply() as an alternative

I am new to R and trying to prepare for an exam in R which will take place in one week. On one of the homework questions, I am trying to solve a single problem in as many as ways as possible (preparing more tools always comes in handy in a…
R Newbie
  • 57
  • 6
2
votes
2 answers

assign column names as row names after using lapply

like the question , I want to assign the column names of which were processed by lapply and tapply in R. A simple example: df<-data.frame('X1'=rnorm(100), 'X2'=rnorm(100), …
Jia Gao
  • 1,172
  • 3
  • 13
  • 26
2
votes
1 answer

Aggregate the total revenue for each date by using aggregate function

I have a daily revenue dataset df from 2016-01-01 to 2017-05-21. The dataset contains Datum, languages and Opbrengst variables. Datum lanuage Opbrengst 596 20160101 bg 254 923 20160101 bg-bg 434 1044 20160101 ca …
Sheryl
  • 721
  • 1
  • 9
  • 17
2
votes
1 answer

Using the apply functions to get sum of products of arrays in R

Say I have two three dimensional arrays and I would like the sum of the products of the arrays based on one of the indices. What I would like is that sum in the the last line of the example code below. I know I can use a loop but I'd like to do this…
statsgrad1990
  • 89
  • 1
  • 3
2
votes
1 answer

How to call a function using *apply with different data, where output is list and intput is list of 2x2 matrix?

I have data from 10 runs of a simulation, stored as a list. I want to call a function FUN1 with this data, without repeating the code 10 times. The output from FUN1 is a value of model parameter such that: $theta=5 …
F.family
  • 47
  • 8
2
votes
2 answers

Adding tapply to a column

I have a data frame with 2 columns: 'sales' and 'month'. I want to add a column that ranks the sales within each month. Does anyone know what's the most succinct way of doing so? I'm thinking of 'tapply' but it gives me a list and I can't add it…
user2864154
  • 455
  • 2
  • 6
  • 15
2
votes
1 answer

How to use ggplot2 in R when the data is a function of tapply

I have a variable that is a function of tapply meanx <- with(dat2, tapply(x, list(type,status), mean)) The out put reads as follows : Status 1 Status2 Type 1 11.99 9.8 Type 2 88.8 100 I also have confidence intervals for the…
user4021557
2
votes
2 answers

R- tapply doesn't keep dates formated

I need to do a fast aggregation by id_client of dates: min, max, difference of dates in months and quantity of months. Example table: tbl<-data.frame(id_cliente=c(1,1,1,1,2,3,3,3), fecha=c('2013-01-01', '2013-06-01','2013-05-01', '2013-04-01',…
GabyLP
  • 3,649
  • 7
  • 45
  • 66
2
votes
1 answer

Difficulty in grouping variables and apply function to groups

I need to group my data into 2 or 3 levels of selection and apply a function on each specific group. The command tapply does it when I use a standard function (mean, median, SD), but when I insert more than one filter variable it does not work. The…
2
votes
2 answers

R - apply adf.test by group

I have a data.frame bbm with variables ticker, variable and value. I want to apply an Augmented Dickey Fuller test via the adf.test function grouped by ticker and variable. R should add a new column to the initial data.frame with the corresponding…
Christoph
  • 101
  • 1
  • 9