Doubt about concatenation in the R language

Asked

Viewed 3,974 times

0

I want to use these variables that I’m importing from a table in my function,gecode.

The geocode function behaves like this: geocode("Av. Paulista, 1578 - Bela Vista, Sao Paulo, Brazil")

I wanted to know if you have any in the R of I use concatenated variables as in PHP I use points.

   library(ggmap)
    library(leaflet)
    library(readxl)

     # no RStudio o diretorio de trabalho corrente do R aponta para o local
    # aonde o script está
    setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

    # le arquivo excel capitais
    Pontos = read_excel("pontos_turisticos.xlsx")  

    nome = Pontos$Nome;
    bairro = Pontos$Bairro;
    endereco = Pontos$Endereço;
    cidade = "Nova Friburgo";
    estado = "RJ";



     geocode("nome[1],bairro[1],endereco[1],cidade,estado");

1 answer

2


In R there is a function called Paste() I don’t know if it’s the right one, but it’s always been there for me.

>paste("Eu", "Quero", "Concatenar")
[1] Eu Quero Concatenar

By default the function uses white space to concatenate vector, but you can use the parameter Collapse

>paste(str, collapse = " ")
[1] Eu Quero Concatenar

In your case:

>paste(c(nome[1], bairro[1]), collapse = " ")

or

>str <- c(nome[1], bairro[1])
>paste(str, collapse = " ")

I hope I’ve helped

  • In this case it is not the collapse, is the sep. Try with sep = "".

  • Then I got it wrong I thought he wanted to concatenate and put point, and just put the Collapse = " . " , which concatenates and in the space luga puts "."

  • 1

    Okay, I read your code wrong, if that’s all paste(nome[1], bairro[1]) without the function c(), then it’s up to sep, but so it should be collapse. If you want to concatenate integer vectors it will be better paste(nome, bairro).

  • Yeah, no doubt about it. @Noisy

  • The Paste() was very useful in my case!

Browser other questions tagged

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