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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:13:37+00:00 2026-05-30T21:13:37+00:00

I am having trouble figuring out exactly how to approach this problem and am

  • 0

I am having trouble figuring out exactly how to approach this problem and am hoping someone has an idea. For the following code, I want the result given below. The result are based on the following criteria:

Within the same date:
idEffectA:
Always 0 for rows with id A
1 for “non id A” rows if there is any id A row with a 1 in special for that date
0 for “non id A” rows if all id A rows have a 0 in special for that date

sizeEffect10:
Always 0 for rows with size 10
1 for “non size 10” rows if there is any size 10 row with a 1 in special for that date
0 for “non size 10” rows if all size 10 have a 0 in special for that date

Similarly for other variables. If the names of the columns could also be generated, that would be extremely helpful rather than having to define each one by hand. Also, in the actual data set there are many different categories for size and id, so avoiding hand inputting those into a function would be best, though if that is the only possibility, an aggregate function used with a merge could be used on the data set with the defined function (any other suggestions?). I would like the result columns to be bound to the original data set.

Please let me know if there are any questions as I had a hard time actually defining what I was wanting in the columns to begin with. I have tried using the plyr package along with indexing but haven’t gotten very far. Thanks!

For the first part, I think something like this could work within a loop:

i=0  
ifelse(id==A & max(special[id=="A" & date==min(date)+i], 1, 0)  
i=i+7  

but, after that I’m not really sure…

     original.data
     label  date    special size    id
     1  1/11/2012   0   10  A
     2  1/11/2012   1   20  A
     3  1/11/2012   0   10  B
     4  1/11/2012   0   30  C
     5  1/11/2012   0   10  C
     1  1/18/2012   0   10  A
     2  1/18/2012   0   20  A
     3  1/18/2012   0   10  B
     4  1/18/2012   1   30  C
     5  1/18/2012   1   10  C
     1  1/25/2012   1   10  A
     2  1/25/2012   1   20  A
     3  1/25/2012   0   10  B
     4  1/25/2012   1   30  C
     5  1/25/2012   1   10  C
     1  2/1/2012    0   10  A
     2  2/1/2012    1   20  A
     3  2/1/2012    1   10  B
     4  2/1/2012    0   30  C
     5  2/1/2012    0   10  C
     1  2/8/2012    0   10  A
     2  2/8/2012    0   20  A
     3  2/8/2012    0   10  B
     4  2/8/2012    1   30  C
     5  2/8/2012    0   10  C
     1  2/15/2012   1   10  A
     2  2/15/2012   1   20  A
     3  2/15/2012   0   10  B
     4  2/15/2012   1   30  C
     5  2/15/2012   0   10  C

Here are the results I am looking for:

     results
     idEffectA  sizeEffect10    idEffectB   sizeEffect20    idEffectC   sizeEffect30
     0  0   0   1   0   0
     0  0   0   0   0   0
     1  0   0   1   0   0
     1  0   0   1   0   0
     1  0   0   1   0   0
     0  0   0   0   1   1
     0  1   0   0   1   1
     0  0   0   0   1   1
     0  1   0   0   0   0
     0  0   0   0   0   1
     0  0   0   1   1   1
     0  1   0   0   1   1
     1  0   0   1   1   1
     1  1   0   1   0   0
     1  0   0   1   0   1
     0  0   1   1   0   0
     0  1   1   0   0   0
     1  0   0   1   0   0
     1  1   1   1   0   0
     1  0   1   1   0   0
     0  0   0   0   1   1
     0  0   0   0   1   1
     0  0   0   0   1   1
     0  0   0   0   0   0
     0  0   0   0   0   1
     0  0   0   1   1   1
     0  1   0   0   1   1
     1  0   0   1   1   1
     1  1   0   1   0   0
     1  0   0   1   0   1

Ok, here is the beginning to what I have:

x <- rep(0, length(id)) 
i=min(date) 
n=1 
id.level = 1 

for(i in min(date):max(date)){
    for(id.level in 1:length(levels(id))){
        for(n in 1:length(id)){
            x[n] <- ifelse(max(special[id==id[id.level] & date==i])==1, 0, 1)
            n=n+1
        }
        y <- paste("idEffect",id[id.level]) 
        id.level = id.level + 1
        colnames(x)[length(colnames(x))] <- y
    }
    i=i+7
}

What I was hoping to do with this code was create the 0’s for all id’s such as I wrote in the original post:

idEffectA:
Always 0 for rows with id A

and create a column for each separate id. However, I don’t know what to do for the other condition or how to fit them in. Should I add in nested ifelse statements? Any help greatly appreciated. I’m more used to working with indexing in R so my knowledge of ifelse and for statements is fuzzy. Thanks.

  • 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-30T21:13:38+00:00Added an answer on May 30, 2026 at 9:13 pm

    First, get the data into a reproducible form (I used dput() on original.data that I read in from what you had and did some conversions on). This makes sure that all the data types are right (date is of type Date and id is a factor, etc.; these things are relevant.)

    original.data <-
    structure(list(label = c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 
    5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 
    1L, 2L, 3L, 4L, 5L), date = structure(c(15350, 15350, 15350, 
    15350, 15350, 15357, 15357, 15357, 15357, 15357, 15364, 15364, 
    15364, 15364, 15364, 15371, 15371, 15371, 15371, 15371, 15378, 
    15378, 15378, 15378, 15378, 15385, 15385, 15385, 15385, 15385
    ), class = "Date"), special = c(0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
    1L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 
    0L, 1L, 1L, 0L, 1L, 0L), size = c(10L, 20L, 10L, 30L, 10L, 10L, 
    20L, 10L, 30L, 10L, 10L, 20L, 10L, 30L, 10L, 10L, 20L, 10L, 30L, 
    10L, 10L, 20L, 10L, 30L, 10L, 10L, 20L, 10L, 30L, 10L), id = structure(c(1L, 
    1L, 2L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 
    2L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 1L, 2L, 3L, 3L), .Label = c("A", 
    "B", "C"), class = "factor")), .Names = c("label", "date", "special", 
    "size", "id"), row.names = c(NA, -30L), class = "data.frame")
    

    Next, you want to do processing “Within the same date”. This implies a split-apply-combine strategy. The plyr library handles this well.

    library("plyr")
    

    You want ddply (data.frame in, data.frame out) and you need a function that does your transformations for a subset of the data which corresponds to a single date.

    Using your first two examples (idEffectA and sizeEffect10), implementing your rules would look like:

    ddply(original.data, .(date), function(DF) {
        # idEffectA
        others <- if(any(DF$special[DF$id == "A"] == 1)) {1} else {0}
        DF$idEffectA <- ifelse(DF$id == "A", 0, others)
        # sizeEffect10
        others <- if(any(DF$special[DF$size == 10] == 1)) {1} else {0}
        DF$sizeEffect10 <- ifelse(DF$size == 10, 0, others)
        DF
    })
    

    For idEffectA, others checks if any of the special values corresponding to a value of A are 1, and is 1 if so, 0 otherwise. The assignment to idEffectA then is conditional on whether it is A (0), or not (whatever others was determined to be). Repeat logic for size, but comparing to a number.

    Your further example indicates that you want a column for each possible value of id and size. Loops can get you there.

    allid <- levels(original.data$id)
    allsize <- unique(original.data$size)
    ddply(original.data, .(date), function(DF) {
        for (e in allid) {
            others <- if(any(DF$special[DF$id == e] == 1)) {1} else {0}
            DF[[paste("idEffect",e,sep="")]] <- ifelse(DF$id == e, 0, others)
        }
        for (e in allsize) {
            others <- if(any(DF$special[DF$size == e] == 1)) {1} else {0}
            DF[[paste("sizeEffect",e,sep="")]] <- ifelse(DF$size == e, 0, others)
        }
        DF
    })
    

    which gives

       label       date special size id idEffectA idEffectB idEffectC sizeEffect10 sizeEffect20 sizeEffect30
    1      1 2012-01-11       0   10  A         0         0         0            0            1            0
    2      2 2012-01-11       1   20  A         0         0         0            0            0            0
    3      3 2012-01-11       0   10  B         1         0         0            0            1            0
    4      4 2012-01-11       0   30  C         1         0         0            0            1            0
    5      5 2012-01-11       0   10  C         1         0         0            0            1            0
    6      1 2012-01-18       0   10  A         0         0         1            0            0            1
    7      2 2012-01-18       0   20  A         0         0         1            1            0            1
    8      3 2012-01-18       0   10  B         0         0         1            0            0            1
    9      4 2012-01-18       1   30  C         0         0         0            1            0            0
    10     5 2012-01-18       1   10  C         0         0         0            0            0            1
    11     1 2012-01-25       1   10  A         0         0         1            0            1            1
    12     2 2012-01-25       1   20  A         0         0         1            1            0            1
    13     3 2012-01-25       0   10  B         1         0         1            0            1            1
    14     4 2012-01-25       1   30  C         1         0         0            1            1            0
    15     5 2012-01-25       1   10  C         1         0         0            0            1            1
    16     1 2012-02-01       0   10  A         0         1         0            0            1            0
    17     2 2012-02-01       1   20  A         0         1         0            1            0            0
    18     3 2012-02-01       1   10  B         1         0         0            0            1            0
    19     4 2012-02-01       0   30  C         1         1         0            1            1            0
    20     5 2012-02-01       0   10  C         1         1         0            0            1            0
    21     1 2012-02-08       0   10  A         0         0         1            0            0            1
    22     2 2012-02-08       0   20  A         0         0         1            0            0            1
    23     3 2012-02-08       0   10  B         0         0         1            0            0            1
    24     4 2012-02-08       1   30  C         0         0         0            0            0            0
    25     5 2012-02-08       0   10  C         0         0         0            0            0            1
    26     1 2012-02-15       1   10  A         0         0         1            0            1            1
    27     2 2012-02-15       1   20  A         0         0         1            1            0            1
    28     3 2012-02-15       0   10  B         1         0         1            0            1            1
    29     4 2012-02-15       1   30  C         1         0         0            1            1            0
    30     5 2012-02-15       0   10  C         1         0         0            0            1            1
    

    I pre-compute the possible values of id and size (allid and allsize) since that only needs to be done once. Within the function for each date, iterate over each possible value of id. Build the “Effect” columns the same way, but using paste to create the column name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a little trouble figuring out exactly how const applies in a specific
I'm having a bit of trouble figuring out exactly why my report will not
I'm having trouble figuring out how to exclude /public/bin from this rewrite rule RewriteCond
I'm having trouble figuring out how to represent the following JPQL query: SELECT count(e)
I'm having a bit of trouble figuring out what would be the best approach
I having trouble figuring out the correct architecture for this kind of application: it's
I was having trouble figuring out the XPATH of the following. There are several
I am having trouble figuring out the reason why my .c code is having
I have been having some trouble figuring out how exactly I get a process's
I'm having some conceptual trouble on figuring out how to best implement this... I

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.