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
1
vote
1 answer

How to use the apply() family to identify column names of max values across rows

I'd like to add to a dataframe a column stating the names of those columns in which the maximum value computed across rows in the dataframe is located. Let's say I have this dataframe: set.seed(123) df <- data.frame( V1 = rnorm(10), V2 =…
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
1
vote
1 answer

Using approx function within tapply or by in R

I have a temperature profiler (tp) data for date, depth and temperature. The depth for each date is not exactly the same so I need to unify it to the same depth and set the temperature for that depth by linear approximation. I was able to do this…
Shajar
  • 87
  • 6
1
vote
2 answers

R - Applying same function on multiple columns

This is my first time asking a question here and I'm a beginner at R. I have a huge dataset, where I want to get some overview of the values of multiple columns, based on…
keep_swimming
  • 31
  • 1
  • 6
1
vote
1 answer

tapply returns NA for every level of the factor index or insists the object and index are different lengths

I'm trying to use tapply to get the average weight of turtles caught per day. tapply returns NA for every date value (class:POSIXct) for every approach I've tried I've tried: calling tapply on the weight column and date column -> arguments are…
smbritton
  • 11
  • 3
1
vote
3 answers

Rolling average for panel data (with a few details)

I have come up with some code to calculate a rolling mean for panel data (a row in the data contains values of one subject from one day). Since I had a few more specific requirements the code became quite complicated. Too complicated for an…
MrMax
  • 393
  • 1
  • 2
  • 10
1
vote
3 answers

How to subset multiple columns condition in R?

All, My dataset looks like following. I am trying to answer below question. Question: Based on Drawing paper data ONLY, does the stores sells more units (units.sold column) of one paper subtype(paper.type) than others ? To answer above question I…
Data_is_Power
  • 765
  • 3
  • 12
  • 30
1
vote
2 answers

Replace NA values with median by group

I have used the below tapply function to get the median of Age based on Pclass. Now how can I impute those median values to NA values based on Pclass? tapply(titan_train$Age, titan_train$Pclass, median, na.rm=T)
Suhas U
  • 43
  • 7
1
vote
1 answer

When using the function By in R, how do I create a vector of the factors for each group?

I am using the function "by" in R and at the end, I would like a vector with the factors in it as well because I want to create a data frame that has what I computed and the factor next to it that the function used to subset that particular group.…
I Wright
  • 51
  • 5
1
vote
3 answers

How do I create a new categorical variable from continuous multiple observations?

This is my data: ID dist 1 23 1 10 2 12 2 20 3 14 3 33 I want to go through each ID, and create a new column ("state") for the larger value for each ID call it "high" and for the lower value, call it "low". What's the best way to do…
R-MASHup
  • 365
  • 2
  • 9
1
vote
2 answers

does tapply by default excludes the NA on the variable passed for segmentation?

I have a data frame (a) where two columns are total_amount and Gender. I need to calculate total amount spent by Males and females. In Gender column, there are NAs as well. On running the following command tapply(a$total_amount,a$Gender, sum) The…
Divya
  • 11
  • 1
1
vote
1 answer

Re-organizing, regrouping, and reducing redundancy in R

I have data that looks like this: Hours<-c(.25,.5,.5,.5,.25,.5, 1, .5) WaterYear<-rep(2013:2014,each = 4); WaterYear Events0<-c(1,0,2,2,2,0,3,3) Events1<-c(0,0,0,1,0,0,2,0) Events2<-c(0,0,0,0,0,0,1,0) df<-data.frame(WaterYear, Hours,Events0,Events1,…
novice2382
  • 11
  • 4
1
vote
1 answer

using tapply inside shiny to produce summary outputs

The code below is reproducible: library(shiny) library(Rcpp) library(ggmap) library(htmlwidgets) library(leaflet) crime2 <- crime[1:50,] ui <- fluidPage( titlePanel("Unusual Observations"), sidebarLayout( sidebarPanel( …
glor
  • 109
  • 1
  • 7
1
vote
1 answer

Nest apply function within tapply

I would like to use tapply to group the outcome of a function according to a variable. The function that I think I need to pass to tapplyis, I believe, apply. I want this function to assign a value depending on the presence of another value in a…
pd441
  • 2,644
  • 9
  • 30
  • 41
1
vote
1 answer

How to use lapply or a family of the apply function for calling a function within a function in R?

How to use lapply or a family of the apply function for calling a function within a function? I have a parent function (i.e., hrat) that calls a sister function (i.e., drat) within it. I would like to apply this function over certain vector. I am…
Cricketer
  • 399
  • 1
  • 3
  • 20
1
vote
1 answer

Tapply and function with several arguments

I can use the tapply function to make basic operations (e.g. using mtcars data, calculate mean weight by number of cylinders). library(data.table) mtcars <- data.table(mtcars) tapply(X = mtcars[,wt], INDEX = mtcars[,cyl], …
user3507584
  • 3,246
  • 5
  • 42
  • 66