0

I have two data types price of type list and company of type character

price
[[1]]
[1] "Rp 83.000"

[[2]]
[1] "Rp 87.900"

[[3]]
[1] "Rp92,000"

[[4]]
[1] "Regular price  125,000.00 Discount price  125,000.00 "

and

company
[1] "Tororo"     "Babyzania"  "Bilna"      "Babylonish"

I want to create a data frame using the two data types and of the form :

Tororo Rp 83.00

Babyzania Rp 87.900

Bilna Rp 92,00

Babylonish Regular price 125,000.00 Discount price 125,000.00

Can anyone help me out i tried using directly

df=data.frame(company,price)

But it is not giving the correct dataframe I want.

1
  • Try to unlist() the price variable first. It's not a vector like company. Commented Aug 7, 2015 at 20:14

2 Answers 2

3

You can use this approach:

The data:

price <- list("Rp 83.000", "Rp 87.900", "Rp92,000",
              "Regular price  125,000.00 Discount price  125,000.00 ")
company <- c("Tororo", "Babyzania", "Bilna", "Babylonish")

Use regular expressions to create the price strings:

price2 <- sub(".*?([0-9.,]+) *$", "Rp \\1", unlist(price))
# [1] "Rp 83.000"     "Rp 87.900"     "Rp 92,000"     "Rp 125,000.00"

Create data frame:

data.frame(company, price = price2)
#      company         price
# 1     Tororo     Rp 83.000
# 2  Babyzania     Rp 87.900
# 3      Bilna     Rp 92,000
# 4 Babylonish Rp 125,000.00
Sign up to request clarification or add additional context in comments.

4 Comments

For Babylonish price output should belike this "Regular price 125,000.00 Discount price 125,000.00 "
@HimaanshuGauba You changed the expected output after I posted my question.
sorry actually I mistakenly wrote the wrong output for Babylonish
@HimaanshuGauba You don't need to apply sub. The function unlist is sufficient.
1

If your list is exactly the same length and in the same order as your company name vector you can do this

df = data.frame(company, unlist(price))

Your 4th price is very different from the top 3 so you wont get what you printed anyway.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.