La concatenación de elementos de dos vectores significa tomar simultáneamente valores de dos vectores y unirlos o concatenarlos en uno. Para esto, la función paste() se usa en el lenguaje de programación R.
Sintaxis:
pegar(vector1,vector2,….,vectorn)
Donde, los vectores son las entradas para pegar la función
Ejemplo 1:
R
# create a vector named as first_names first_names=c("sravan","bobby","mohan","sai") # create a vector named as middle_names middle_names=c("kumar","kumari","pavan","kumar") # apply paste function print(paste(first_names,middle_names))
Producción:
[1] “sravan kumar” “bobby kumari” “mohan pavan” “sai kumar”
Podemos combinar n elementos vectoriales a la vez
Ejemplo: 2
R
# create a vector named as first_names first_names=c("sravan","bobby","mohan","sai") # create a vector named as middle_names middle_names=c("kumar","kumari","pavan","kumar") # create a vector named as middle_names last_names=c("H","B","G","M") # apply paste function print(paste(first_names,middle_names,last_names))
Producción:
[1] “sravan kumar H” “bobby kumari B” “mohan pavan G” “sai kumar M”
Si no necesitamos dejar un espacio entre dos strings concatenadas, use la función paste0() .
Sintaxis:
pegar0(vector1,vector2,…,vectorn)
Ejemplo:
R
# create a vector named as first_names first_names=c("sravan","bobby","mohan","sai") # create a vector named as middle_names middle_names=c("kumar","kumari","pavan","kumar") # create a vector named as middle_names last_names=c("H","B","G","M") # apply paste0 function fro combining first and middlename print(paste0(first_names,middle_names)) # apply paste0 function fro combining first middlename and last names print(paste0(first_names,middle_names,last_names))
Producción:
[1] “sravankumar” “bobbykumari” “mohanpavan” “saikumar”
[1] “sravankumarH” “bobbykumariB” “mohanpavanG” “saikumarM”
Publicación traducida automáticamente
Artículo escrito por pulamolusaimohan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA