How to extract only the first value from a row of concatenated values?

Asked

Viewed 64 times

2

I have the database below, and would like to capture only the first value of each line.

a <-  data.frame('Exports' = c("c(182752.414, 179107.7)",
                            "c(200491.435, 195097.2)",
                            "c(217566.642, 211641.4)"))

Expected output:

              Exports   
            182752.414
            200491.435
            217556.642
  • 2

    Please, Roney, you’re already an experienced user xkcd, can you please, edit the question with the departure of dput(dados) or, if the base is too large, dput(head(dados, 20))? Note: dados is the base name, for example a data frame..

  • 1

    I voted to close the issue. The AP has been using the for almost a month intensely and insists on creating questions that are not good, even being warned of it.

  • @Marcus Nunes, good afternoon, how to qualify if a question is good or not? If I’m on the site to seek answers I don’t know, and according to that replicate the need for people who have similar questions, I think we all have questions and if I’m asking questions it’s because I want to develop, I think that people with greater knowledge should collaborate with the cause, if you have wrong at some point please signal me that I will definitely reformulate my questioning, I am here and I am new in the handling of the site, I make the caveat that I am here to learn from many others.

  • 1

    I already answered that here.

  • I don’t understand, the questions are different.

  • 1

    Please do not use comments for debates. The question of acceptable questions is one of the long discussions of the Meta. But the question of provide reproducible example other than image is a topic more than solved.

  • OK @Carloseduardolagosta, I did not want to open strand for more heated discussions, I apologize for providing a question in a way not consistent with the guidelines of the site, I thank you for the signage and learning.

Show 2 more comments

1 answer

5


A regular expression that captures the content between strings resolves:

a <-  c("c(182752.414, 179107.7)", "c(200491.435, 195097.2)", "c(217566.642, 211641.4)")

as.numeric(sub("^c\\((.*),.*", "\\1", a))
#> [1] 182752.4 200491.4 217566.6

Understanding the expression: ^ indicates the beginning of string; c\\( corresponds to "c(", the bars are to indicate that the parenthesis is to be interpreted literally, and not as part of the expression; (.*) indicates the portion to be captured, in this case all before ,.* (comma followed by everything else). The \\1 indicates to keep the first group (the part bounded by parentheses)

  • Thanks for the @Carlos Eduardo Lagosta class!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.