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

  • Home
  • SEARCH
  • 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 8966649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:04:01+00:00 2026-06-15T17:04:01+00:00

I have the following table ordered group by first, second and name. myData <-

  • 0

I have the following table ordered group by first, second and name.

    myData <- structure(list(first = c(120L, 120L, 126L, 126L, 126L, 132L, 132L), second = c(1.33, 1.33, 0.36, 0.37, 0.34, 0.46, 0.53), 
    Name = structure(c(5L, 5L, 3L, 3L, 4L, 1L, 2L), .Label = c("Benzene", 
    "Ethene._trichloro-", "Heptene", "Methylamine", "Pentanone"
    ), class = "factor"), Area = c(699468L, 153744L, 32913L, 
    4948619L, 83528L, 536339L, 105598L), Sample = structure(c(3L, 
    2L, 3L, 3L, 3L, 1L, 1L), .Label = c("PO1:1", "PO2:1", "PO4:1"
    ), class = "factor")), .Names = c("first", "second", "Name", 
    "Area", "Sample"), class = "data.frame", row.names = c(NA, -7L))

Within each group I want to extract the area that correspond to the specific sample. Several groups don´t have areas from the samples, so if the sample is´nt detected it should return “NA”.Ideally, the final output should be a column for each sample.

I have tried the ifelse function to create one column to each sample:

PO1<-ifelse(myData$Sample=="PO1:1",myData$Area, "NA")

However this doesn´t takes into account the group distribution. I want to do this, but within the group. Within each group (a group as equal value for first, second and Name columns) if sample=PO1:1, Area, else NA.

For the first group:

structure(list(first = c(120L, 120L), second = c(1.33, 1.33), 
Name = structure(c(1L, 1L), .Label = "Pentanone", class = "factor"), 
Area = c(699468L, 153744L), Sample = structure(c(2L, 1L), .Label = c("PO2:1", 
"PO4:1"), class = "factor")), .Names = c("first", "second", "Name", 
"Area", "Sample"), class = "data.frame", row.names = c(NA, -2L))

The output should be:

structure(list(PO1.1 = NA, PO2.1 = 153744L, PO3.1 = NA, PO4.1 = 699468L), .Names =c("PO1.1", "PO2.1", "PO3.1", "PO4.1"), class = "data.frame", row.names = c(NA, -1L))

Any suggestion?

  • 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-06-15T17:04:02+00:00Added an answer on June 15, 2026 at 5:04 pm

    As in the example in the quesiton, I am assuming Sample is a factor. If this is not the case, consider making it such.

    First, lets clean up the column Sample to make it a legal name, or else it might cause errors

    levels(myData$Sample)  <-  make.names(levels(myData$Sample))
    
    
    ## DEFINE THE CUTS##
    
    # Adjust these as necessary
    #--------------------------
      max.second <- 3  #  max & nin range of myData$second 
      min.second <- 0  #
      sprd <- 0.15     # with spread for each group
    #--------------------------
    
    # we will cut the myData$second according to intervals,   cut(myData$second, intervals)
    intervals <- seq(min.second, max.second, sprd*2)
    
    # Next, lets create a group column to split our  data frame by 
    myData$group <- paste(myData$first, cut(myData$second, intervals), myData$Name, sep='-') 
    groups <- split(myData, myData$group)
    
    samples <- levels(myData$Sample)   ## I'm assuming not all samples are present in the example.  Manually adjusting with: samples <- sort(c(samples,  "PO3.1"))
    
    
    # Apply over each group, then apply over each sample    
    myOutput <- 
      t(sapply(groups, function(g) {
    
          #-------------------------------
          # NOTE: If it's possible that within a group there is more than one Area per Sample, then we have to somehow allow for thi. Hence the "paste(...)"
          res <- sapply(samples, function(s) paste0(g$Area[g$Sample==s], collapse=" - "))  # allowing for multiple values
          unlist(ifelse(res=="", NA, res))
    
          ## If there is (or should be) only one Area per Sample, then remove the two lines aboce and uncomment the two below:
          # res <- sapply(samples, function(s) g$Area[g$Sample==s])  # <~~ This line will work when only one value per sample
          # unlist(ifelse(res==0, NA, res))
          #-------------------------------
    
      }))
    
    # Cleanup names
    rownames(myOutput) <- paste("Group", 1:nrow(myOutput), sep="-")  ## or whichever proper group name
    
    # remove dummy column 
    myData$group <- NULL
    

    Results

    myOutput
    
            PO1.1    PO2.1    PO3.1 PO4.1            
    Group-1 NA       "153744" NA    "699468"         
    Group-2 NA       NA       NA    "32913 - 4948619"
    Group-3 NA       NA       NA    "83528"          
    Group-4 "536339" NA       NA    NA               
    Group-5 "105598" NA       NA    NA        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table: Table +----+------+-------+ | ID | Name | Group |
Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
I have the following table drop table names; create table names( name varchar(200), surname
I have the following table that lists all awarded cups: Name: Cups Columns: Month,
we have following table (output is already ordered and separated for understanding): | PK
I have the following table but I havent been able to group it, by
I have the following 3 classes(mapped to sql tables). Places table: Name(key) Address Capacity
I have two tables that have the following structure: Table 1 : Show the
I have a table structure similar to the following: ID |DateUploaded|FamilyCode| 1 |01/01/2010 |FC0001
I have table called comments with following structure: id | entry_id | date |

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.