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.

330 questions
-1
votes
1 answer

Is it necessary to use factor to INDEX argument for tapply in r?

x #X Income Commute Job.Growth Physicians #1 A 26000 49.2 10.8 1987 #2 B 29300 45.3 9.5 517 #3 C 24800 39.8 8.2 592 #4 D 27900 46.8 7.6 3310 #5 E 37500 39.9 12.2 …
student.J
  • 25
  • 4
-1
votes
1 answer

R: Extract non-NA elements from a matrix and return with row/column labels

I have a large matrix as a result of using tapply with an INDEX argument of two rows from a dataframe. Most of the matrix is empty (NA). Here is how I used tapply: latavgs <- tapply(geodata$latitude,geodata[5:6],FUN=mean) where latavgs is my…
-1
votes
1 answer

Tapply only producing missing values

I'm trying to generate estimates of the percent of Catholics within a given municipality in a country and I'm using multilevel regression and post-stratification of survey data. The approach fits a multilevel logit and generates predicted…
-1
votes
1 answer

Passing arguments to a list in tapply

I'm new to R and having trouble with passing arguments to a function for a few days now. I have a function that uses tapply with a by statement in it, My dataset is in the form of list data frames,so the function is: Means =…
user2721827
  • 163
  • 1
  • 2
  • 7
-2
votes
1 answer

Using R to calculate the mean of the part of a vector

My R vector looks like this: vector <- c(3, 2, 1, 4, 6, 2, 7) I want to use the function tapply() to calculate the mean of the first 4 number from the vector. How do I do it? What did I do? tapply(vector(1,4), mean) but it seems like it does not…
floss
  • 1,487
  • 1
  • 11
  • 26
-2
votes
1 answer

Use function in loop over set of files in directory

I'm trying to do some data analysis as follows: I have about 100 subjects, each of whom have a file containing 40,000 lines of numbers. I also have an index file with 40,000 corresponding lines containing group number. I am trying to get the means…
Jake
  • 3
  • 2
-2
votes
1 answer

Using tapply on a list of data frames

I would like to use tapply on a list of data frames in oder to calculate sums for individual groups and then tabulate the occurence of the value 0. On an individual data frame I would do this: sums <- tapply(my_data_frame$V3, my_data_frame$V2,…
user10198023
-2
votes
2 answers

how to use tapply for repeated data in R

this is my dataframe: x day month 5 1 1 4 1 1 1 2 1 3 2 1 5 1 2 2 1 2 5 2 2 3 2 2 I need to take the sum of x values for each day in each month. I already have tried: tapply(DF$x, DF$day, max) but it is not giving the right answers.
Madamespring
  • 35
  • 1
  • 6
-2
votes
1 answer

R how to do multiple GLMs for each level for a factor in my data.frame?

Is there an appropriate way to do R style logistic regression where I avoid using loops, but I can have multiple regressions for each level in a particular factor? For example, assume df is 365 daily rows with a binary to say whether or not it…
JoeBass
  • 449
  • 7
  • 15
-2
votes
2 answers

tapply with non numeric values

I'm in a class learning R right now, and I'm stuck on a problem. We set up the following data frame: Column1=c(rep("control",300),rep("ConditionB",300),rep("ConditionC",300)) Column2=(900,mean=100,sd=10) data=data.frame(Column1,Column2) It runs…
-2
votes
3 answers

To pass the row number to another column

I have a dataframe. I need to find a minimum value in the 1st column for each value of the 2nd column. But I should return the value in the 3rd column from the same row as the minimum found in the 1st column. The first part seems is solved by…
-3
votes
2 answers

Print the 3rd number and all the elements divisible by 2

nums <- 1:20 result <- nums[fill the function] result Please anyone can tell me what function to use
-3
votes
2 answers

Group pairwise categories together

I am currently working with a dataset where loans are displayed with a purpose for the loan and an associated loan grade for each loan. The dataset is called loancase and one of the columns is the purpose while another column is grade. Below I have…
user1713336
  • 81
  • 5
  • 14
-3
votes
1 answer

using tapply in tapply

I have a big data.frame with genomic data. The data looks like this- colnames(df)=c("id","chr","start","end","log2") where id is the sample name, chr is the number of the chromosome, start and end give me the location on the chromosome, and log2 is…
T.G.
  • 544
  • 3
  • 5
  • 25
-7
votes
1 answer

multiply multiple column and find sum of each column for multiple values

I'm trying to multiply column and get its names. I have a data frame: v1 v2 v3 v4 v5 0 1 1 1 1 0 1 1 0 1 1 0 1 1 0 I'm trying to multiplying each column with other, like: v1v2 v1v3 v1v4 v1v5 and v2v3 v2v4 v2v5 etc,…
vct
  • 65
  • 10
1 2 3
21
22