Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6761377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:10:56+00:00 2026-05-26T14:10:56+00:00

I have a dataframe: > df <- data.frame( + Species = rep(LETTERS[1:4], times=c(5,6,7,6)), +

  • 0

I have a dataframe:

> df <- data.frame(
+   Species = rep(LETTERS[1:4], times=c(5,6,7,6)),
+   Length = rep(11:14, each=3) 
+ )
> 
> df

I need to be able to count the number of individuals of a certain Length for each Species (i.e., how many individuals in Species A have a length of 1, 2, 3, etc?) Then, I need to perform a series of additional analyses on the output. For example, I need to calculate the density of individuals of each length, and the decrease in density from one length class to the next.

This is easy if I subset the data first:

Spec.A<-df[df$Species=="A",] 

#count number of specimens of each length; 
count<-table(Spec.A$Length)
count

#calculate density per length category (divide by total area sampled =30) 
density<-count/(30)
density

#calculate the decrease in density (delta.N) from one length category to the next; 
delta.N<-diff(density, lag=1, differences=1)
delta.N

The problem is that I need to do these calculations for each species (i.e., to loop through each subset).

On the one hand, I could use tapply(), with a function that uses table();

#function: count number of specimens of each length; 
count<-function(x){
table(x)
}

Number<-tapply(df$Length, df$Species, FUN=count, simplify=FALSE)
Number

This gives me what I want, but the format of the output is funky, and I can’t figure out how to perform additional analyses on the results.

I have tried using ddply() from plyr, something like:

ddply(df$Length, df$Species,
count)

But I clearly don’t have it right, and I’m not even sure ddply() is appropriate for my problem, given that I have a different number of length observations for each species.

Should I be looking more closely at other options in plyr? Or is there a way to write a for loop to do what I need?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:10:57+00:00Added an answer on May 26, 2026 at 2:10 pm

    You’re on the right track! tapply with list output is definitely one way to go, and may be a good choice since your outputs will have varying lengths.

    ddply, like you guessed, is another way. The key is that the output of the function you give to ddply should be a data frame with all your statistics in a “long” mode (so that they will stack nicely). The simple count function can’t do this, so you’ll need to make your own function. The way I go about devising a function for a ddply call like this is actually very similar to what you were doing: I get a subset of the data, and then craft my function using that. Then, when you submit it to ddply, it’ll nicely apply that function across all the subsets.

    SpeciesStats <- function(df) {
      counts    = table(df$Length)
      densities = counts/30
      delta.N   = diff(densities, lag=1, differences=1)
    
      data.frame(Length   = names(counts),
                 Count    = as.numeric(counts),
                 Density  = as.numeric(densities),
                 delta.N  = c(NA, delta.N), 
                 row.names=NULL)
    }
    
    > ddply(df, 'Species', SpeciesStats)
       Species Length Count    Density     delta.N
    1        A     11     3 0.10000000          NA
    2        A     12     2 0.06666667 -0.03333333
    3        B     12     1 0.03333333          NA
    4        B     13     3 0.10000000  0.06666667
    5        B     14     2 0.06666667 -0.03333333
    6        C     11     3 0.10000000          NA
    7        C     12     3 0.10000000  0.00000000
    8        C     14     1 0.03333333 -0.06666667
    9        D     13     3 0.10000000          NA
    10       D     14     3 0.10000000  0.00000000
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
Let's have a dataframe with long strings in one column: df<-data.frame(short=rnorm(10,0,1),long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse=))) How could I
I have a dataframe with numeric entries like this one test <- data.frame(x =
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I have a data.frame called series_to_plot.df which I created by combining a number of
If you have a dataframe like this mydf <- data.frame(firstcol = c(1,2,1), secondcol =
I have a data frame where each line represents an individual. That data frame
Suppose I have the following dataframe: df <- data.frame(BR.a=rnorm(10), BR.b=rnorm(10), BR.c=rnorm(10), USA.a=rnorm(10), USA.b =
I have a data.frame like this: (t=structure(list(count = c(NA, 2, NA, NA, NA, 8,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.