Questions tagged [plyr]

plyr is an R package with tools to solve a variety of problems using the split-apply-combine strategy

plyr is an R package written by Hadley Wickham which contains tools to solve a variety of problems using the strategy of split, apply and combine:

  • Split a data structure (data frame, list, array) into smaller pieces;
  • Apply a function to each piece; then
  • Combine the results into a data structure.

It partially replaces the apply family of functions (lapply, tapply, Map, etc.) in base-R, and is partially succeeded by .

Repositories

Other resources

Related tags

2465 questions
1
vote
1 answer

Doing several cross sectional regression of unbalanced panel data in R

I have an unbalanced panel data set. The following data will do for illustration: Id <- c(rep(1:4,3),rep(5,2)) Id <- Id[order(Id)] Year <- c(rep(2000:2002,4),c(2000,2002)) z1 <- rnorm(14) z2 <- rnorm(14) z3 <- rnorm(14) z4 <- rnorm(14) CORR <-…
Mace
  • 1,259
  • 4
  • 16
  • 35
1
vote
3 answers

sort a matrix according to some group indices in R

I have a matrix generated as follows: set.seed(999) mat.a = matrix(round(rnorm(24,4,9)),3,8) mat.a The mat.a looks like this: [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 6 -13 -6 12 -8 12 10 [2,] -8 2 -7 16 6 …
alittleboy
  • 10,616
  • 23
  • 67
  • 107
1
vote
1 answer

Apply xtable() to a list of tables and a create pdf using knitr in R-studio

I want to convert a list of tables to Latex using xtable() and create a pdf using Knitr in R-studio. I have tried to use llply() on the list but it does not work. Here is a list of tables: library(plyr) library(xtable) Data <-…
Rasmus Larsen
  • 5,721
  • 8
  • 47
  • 79
1
vote
1 answer

How to compute the mean for the last few rows in each time period in a data frame?

I have data collected for a few subjects, every 15 seconds over an hour split up by periods. Here's how the dataframe looks like, the time is "Temps", subjects are "Sujet" and the periods are determined by "Palier". data.frame': 2853 obs. of …
Jonathan
  • 119
  • 1
  • 6
1
vote
3 answers

Convert a data frame with many time series into a list of single variate xts

I have a data frame of time series: X1.HK.Equity X X2.HK.Equity X.2 X3.HK.Equity X.4 1 31/12/2002 38.855 31/12/2002 19.547 31/12/2002 5.011 2 02/01/2003 38.664 02/01/2003 19.547 02/01/2003 4.986 3 03/01/2003 40.386 …
mchangun
  • 9,814
  • 18
  • 71
  • 101
1
vote
1 answer

Calculate group characteristics with ddply

I have a follow-up question related to Calculate "group characteristics" without ddply and merge I have a similar dataframe (per below), but trying to calculate the percentage of rotten fruits among the other fruits in the same category. I should…
user1885116
  • 1,757
  • 4
  • 26
  • 39
1
vote
0 answers
1
vote
1 answer

Transposing a data frame

I have a question about re-shaping (if that's the right word) a data frame to a transposed version of it. I want to take something like: A B C 1 6 1 1 18 1 1 21 1 3 18 1 3 21 1 4 6 1 4 18 1 4 20 1 4 21 1 And turn…
AI52487963
  • 1,253
  • 2
  • 17
  • 36
1
vote
1 answer

Multiple plyr functions and operations in one statement?

I have a dataset as follows: i,o,c A,4,USA B,3,CAN A,5,USA C,4,MEX C,1,USA A,3,CAN I want to reform this dataset into a form as follows: i,u,o,c A,3,4,2 B,1,3,1 C,2,2.5,1 Here, u represents the unique instances of variable i in the dataset, o =…
Shion
  • 395
  • 1
  • 3
  • 13
1
vote
1 answer

Maximum slope for a given interval each day

I have a set of time series data with ground surface temperatures measured every 10 minutes over multiple days (actually 2 years of data) from three different locations. What I am interested in calculating is the maximum slope (rate of temperature…
Nate Miller
  • 386
  • 5
  • 19
1
vote
2 answers

mapvalues in plyr gives unexpected output when "to" argument is a factor...is it a bug?

When I use mapvalues in the plyr package (plyr v1.8, R v2.15.1 Roasted Marshmallows), I get an odd result when the "to" argument is a factor. For example, v1 = c(1,2,2,1,2) mapvalues(v1, from = c(1, 2), to = factor( c('A', 'B') ) ) returns [1] 1 2…
David Choi
  • 23
  • 3
1
vote
1 answer

creating a data frame with lapply(and plyr package)

I have multiple data files in which im interested in cleaning up then obtaining means from to run repeated measures ANOVA on. Here's example data, in real data theres 4500 rows and another line called Actresponse which sometimes contains a 9 which I…
luke123
  • 631
  • 3
  • 9
  • 15
1
vote
0 answers

Building a table of probability transition matrices with plyr

I'm trying to model a system of continuous time Markov chains where in different time intervals I have different rates. I build a rate matrix for each time period like this make.rate.matrix <- function(c1, c2, m12, m21) { matrix( c(# State 1:…
Thomas Mailund
  • 1,674
  • 10
  • 16
1
vote
1 answer

Not sure why dcast() this data set results in dropping variables

I have a data frame that looks like: id fromuserid touserid from_country to_country length 1 1 54525953 47195889 US US 2 2 2 54525953 54361607 US US 1 3 3 54525953 53571081 US …
Evan Zamir
  • 8,059
  • 14
  • 56
  • 83
1
vote
1 answer

remove repeated elements of a data.frame based on previous and next values in R

First of all, hallo! This is my first post here and I have to admit that I'm bit nervous I have a data.frame where each element is in triplicates and has it own value, here is an example test <-data.frame (id = c("a", "a", "a" , "b", "b", "b"), …
spleen
  • 119
  • 2
  • 10