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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:10:23+00:00 2026-05-18T11:10:23+00:00

I have the following question: I have data frame which looks like this. I

  • 0

I have the following question:

I have data frame which looks like this. I have prices, 3 X’s and 2 R’s.

Date    Name  Price  Interest
01.02.10 X  120     0.2
01.02.10 R  120     0.3
01.02.10 X  130     0.8
01.02.10 X  140     0.4
01.02.10 R  130     0.2
etc.

I would like to tell R to look for pairs of X&Rs with the same price, and delete the rest. So this should result: 2 X’s and 2’Rs (in this case).

Date    Name  Price  Interest
01.02.10 X  120     0.2
01.02.10 R  120     0.3
01.02.10 X  130     0.8
01.02.10 R  130     0.2
etc.

To make it clearer (hopefully): I have a lot of different prices for each date. Each row either has an X or an R in it. There are a lot of pairs on each date, i.e. for example X, Price = 120 & R, Price = 120 on Date 1. But there are also Prices which only match one Name, for example there is a Price = 140 only for Name = X. So what i would like R to do is: check for machting Names for one Price (i.e. there exists the same Price for one X and one R) and delete the rest. What actually would result is the same number of X’s and R’s because I’m looking for pairs.

I’m sorry not to be able to post something I tried. I just couldn’t think of anything.

Now, to the next problem:
If the pairs are there, I would like to tell R to check each line. If the Name is X, I want it to calculate a new price, if not just print the existing price.
I tried

xx <- if(Name == "X"){Price + 100*interest} else print{Price}

but it didn’t work.

Thanks for help

Cheers
Dani

  • 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-18T11:10:24+00:00Added an answer on May 18, 2026 at 11:10 am

    Edit: @Dwin’s comment to the Q was a bit cryptic, and seeing as my first attempt at part 1 of the Q was not correct due to the unclear Q, I’ll try to redeem myself with a go at expanding on DWin’s comment:

    [Assuming dat contains the data you quote in the Q.] First, merge dat with itself:

    > foo <- merge(dat[, -4], dat, by.x = "Date", by.y = "Date")
    > head(foo)
          Date Name.x Price.x Name.y Price.y Interest
    1 01.02.10      X     120      X     120      0.2
    2 01.02.10      X     120      R     120      0.2
    3 01.02.10      X     120      X     130      0.2
    4 01.02.10      X     120      X     140      0.2
    5 01.02.10      X     120      R     130      0.2
    6 01.02.10      R     120      X     120      0.2
    

    Next, get out the rows where Price.x == Price.y and where Name.x != Name.y

    > (foo <- foo[with(foo, which(Price.x == Price.y & Name.x != Name.y)),])
           Date Name.x Price.x Name.y Price.y Interest
    2  01.02.10      X     120      R     120      0.2
    6  01.02.10      R     120      X     120      0.2
    15 01.02.10      X     130      R     130      0.2
    23 01.02.10      R     130      X     130      0.2
    

    Then, get rid of the superfluous columns:

    > (foo <- foo[, -(4:5)])
           Date Name.x Price.x Interest
    2  01.02.10      X     120      0.2
    6  01.02.10      R     120      0.2
    15 01.02.10      X     130      0.2
    23 01.02.10      R     130      0.2
    

    And finally, fix-up the column names:

    > names(foo) <- names(dat)
    > foo
           Date Name Price Interest
    2  01.02.10    X   120      0.2
    6  01.02.10    R   120      0.2
    15 01.02.10    X   130      0.2
    23 01.02.10    R   130      0.2
    

    The second thing can be done using ifelse

    with(dat, ifelse(Name == "X", Price + 100*Interest, Price))
    

    Which gives something this

    > with(dat, ifelse(Name == "X", Price + 100*Interest, Price))
    [1] 140 120 150 160 130
    

    The reason that the if() doesn’t work, is that if() only take a scalar logical (a single TRUE or FALSE), yet Name == "X" returns a logical vector:

    > with(dat, Name == "X")
    [1]  TRUE FALSE  TRUE  TRUE FALSE
    

    In these cases, ifelse() is your friend.

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

Sidebar

Related Questions

I have following question. I'd like to place a file named data.xml into sdcard/appname
Simple question: I have the following markup... <a href='#'> <img src='icon.png'> This is the
I have a situation similar to the following question: Insert Data Into SQL Table
I have the following question regarding Flex/AIR data grids: Can I access the value
I have the following data frame id<-c(1,1,1,1,2,2,2,2,3,3,3,3) time<-c(0,1,2,3,0,1,2,3,0,1,2,3) value<-c(1,1,6,1,2,6,2,2,1,1,6,1) d<-data.frame(id, time, value) The value
I have the following data read into R as a data frame named data_old:
I have the following question: .data a: .word 12,-5,4,0 x: .byte 5 .text main:
I have the following question here .data a: .asciiz 2021 x: .byte 7,2,12 .text
I have a following question: Consider flowing data through pipe to python script and
I have the following question about JPA: Can I save the order of the

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.