Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating strings with

I have a data frame with several variables. What I want is create a string using (concatenation) the variable names but with something else in between them...

Here is a simplified example (number of variables reduced to only 3 whereas I have actually many)

Making up some data frame

 df1 <- data.frame(1,2,3) # A one row data frame
  names(df1) <- c('Location1','Location2','Location3') 

Actual code...

  len1 <- ncol(df1)
  string1 <- 'The locations that we are considering are'  
    for(i in 1:(len1-1))   string1 <- c(string1,paste(names(df1[i]),sep=','))

  string1 <- c(string1,'and',paste(names(df1[len1]),'.'))
  string1

This gives...

[1] "The locations that we are considering are"
[2] "Location1"                          
[3] "Location2"                          
[4] "Location3 ."

But I want

The locations that we are considering are Location1, Location2 and Location3.

I am sure there is a much simpler method which some of you would know... Thank you for you time...

like image 422
Stat-R Avatar asked Mar 22 '12 15:03

Stat-R


People also ask

Can you use += for string concatenation?

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings. In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

What is string concatenation example?

In formal language theory and computer programming, string concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball".

How do I concatenate two characters to a string?

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function. In the above example, we have declared two char arrays mainly str1 and str2 of size 100 characters.

What is concatenating two strings?

The concatenation of strings is a process of combining two strings to form a single string. If there are two strings, then the second string is added at the end of the first string. We can concatenate the strings in the following three ways: Concatenate two strings using loop.


3 Answers

The fact that these are names of a data.frame does not really matter, so I've pulled that part out and assigned them to a variable strs.

strs <- names(df1)
len1 <- length(strs)
string1 <- paste("The locations that we are considering are ", 
                 paste(strs[-len1], collapse=", ", sep=""),
                 " and ",
                 strs[len1], 
                 ".\n", 
                 sep="")

This gives

> cat(string1)
The locations that we are considering are Location1, Location2 and Location3.

Note that this will not give sensible English if there is only 1 element in strs.

The idea is to collapse all but the last string with comma-space between them, and then paste that together with the boilerplate text and the last string.

like image 151
Brian Diggs Avatar answered Oct 12 '22 19:10

Brian Diggs


Are you looking for the collapse argument of paste?

> paste (letters [1:3], collapse = " and ")
[1] "a and b and c"
like image 28
cbeleites unhappy with SX Avatar answered Oct 12 '22 19:10

cbeleites unhappy with SX


If your main goal is to print the results to the screen (or other output) then use the cat function (whose name derives from concatenate):

> cat(names(iris), sep=' and '); cat('\n')
Sepal.Length and Sepal.Width and Petal.Length and Petal.Width and Species

If you need a variable with the string, then you can use paste with the collapse argument. The sprintf function can also be useful for inserting strings into other strings (or numbers into strings).

like image 20
Greg Snow Avatar answered Oct 12 '22 19:10

Greg Snow