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 7674063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:39:21+00:00 2026-05-31T16:39:21+00:00

I have data that I would like to compute some statistics with. The data

  • 0

I have data that I would like to compute some statistics with.
The data is organized in such a way that I have a value corresponding to each 3-element tuple
Something like

(P1,M1,R1,V1)
(P1,M1,R2,V2)
(P1,M2,R1,V1)
...

here P1, M1, and R1 are not numeric but V1 and V2 are.
Right now I have the data in a csv file, x2.cvs as follows:

P,M,R,V
P1,M1,R1,V1
P1,M1,R2,V2
...

I read the data using

d = read.table("x2.csv", sep=",", header=TRUE)

but after that I don’t know what to do to process the data.

I would like to start by computing simple information like:
what is the average for each element of P (so the average would be over all elements
of M and R), or for each pair of elements of {P,M} (so the average here would be over the elements of R.

Next I would like to do a little bit more complicated things like compute how many elements of P1 are bigger than some specified value.

  • 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-31T16:39:22+00:00Added an answer on May 31, 2026 at 4:39 pm

    Here’s a start, with data.table, plyr and base functions, there are so many other ways…

    First, some example data…

    dput(examp)
    structure(list(P = structure(c(1L, 1L, 1L, 2L), .Label = c("P1", 
    "P2"), class = "factor"), M = structure(c(1L, 1L, 2L, 2L), .Label = c("M1", 
    "M2"), class = "factor"), R = structure(c(1L, 2L, 1L, 1L), .Label = c("R1", 
    "R2"), class = "factor"), V = c(23, 49, 24, 29)), .Names = c("P", 
    "M", "R", "V"), row.names = c(NA, -4L), class = "data.frame")
    #
    # to give something like what you have...
    #
    examp
       P  M  R  V
    1 P1 M1 R1 23
    2 P1 M1 R2 49
    3 P1 M2 R1 24
    4 P2 M2 R1 29
    

    Here is one way using data.table. If your data object is very big, you’ll find the data.table package to be very fast, documentation is also excellent: http://datatable.r-forge.r-project.org/datatable-intro.pdf

    # What is the average of each element of P?
    library(data.table)
    examp.dt <- data.table(examp)
    setkey(examp.dt,P)
    examp.dt[,mean(V),by=P]
          P V1
    [1,] P1 32
    [2,] P2 29
    #
    

    And another using plyr

    # What is the average of each element of P?
    library(plyr)
    ddply(examp, "P", function(df)mean(df$V))
    P V1
    1 P1 32
    2 P2 29
    

    And another using base R

    # What is the average of each element of P?
    # for example using the by() function,  tapply() would be similar
    with(examp, by(examp, P, mean))
    P: P1
    P  M  R  V 
    NA NA NA 32 
    ------------------------------------------------- 
    P: P2
    P  M  R  V 
    NA NA NA 29
    #
    # What is the average of each element of R? 
    with(examp, by(examp, R, mean))
    R: R1
    P        M        R        V 
    NA       NA       NA 25.33333 
    ----------------------------------------------
    R: R2
    P  M  R  V 
    NA NA NA 49 
    #
    # the same, using tapply
    with(examp, tapply(V, R, mean)
      R1       R2 
    25.33333 49.00000
    

    And for your last question, how many elements of P1 are bigger than some specified value, we can use subset like so:

    # how many elements of P1 are greater than 20?
    nrow(subset(examp, examp$P=="P1" & examp$V>20))
    [1] 3
    

    Or even just [ for the same result with less typing:

    nrow(examp[examp$P=="P1" & examp$V>20,])
    [1] 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data that I would like to visualize. Each byte of the
I have an Excel table with some data that I would like to export
i have tons of data that i would like to highlight only those cell
Here's the issue: I have 2 data contexts that I would like to do
Ok, so I have an excel spreadsheet that contains data that I would like
I have a view that I would like to populate data when the next
I have a program that reads data from a file line-by-line. I would like
I have some data analysis that needs to perform. On average, it would involve
I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so
I have a Collection of items that I would like to display the Total

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.