2

This works well, but troublesome.

> library(dplyr)
> mutate(iris, a = paste( Petal.Width, Petal.Length) ) %>>% head
Sepal.Length Sepal.Width Petal.Length Petal.Width Species       a
 1          5.1         3.5          1.4         0.2  setosa 0.2 1.4
 2          4.9         3.0          1.4         0.2  setosa 0.2 1.4
 3          4.7         3.2          1.3         0.2  setosa 0.2 1.3
 4          4.6         3.1          1.5         0.2  setosa 0.2 1.5
 5          5.0         3.6          1.4         0.2  setosa 0.2 1.4
 6          5.4         3.9          1.7         0.4  setosa 0.4 1.7

How can I use dplyr's "Select helpers" in paste()?

> mutate(iris, a = paste( starts_with("Petal") ))
Error in mutate_impl(.data, dots) : 
  wrong result size (0), expected 150 or 1
> mutate_(iris, a = paste( starts_with("Petal") ))
Error in parse(text = x)[[1]] : subscript out of bounds
> mutate_(iris, a = paste( starts_with(Petal) ))
Error in is.string(match) : object 'Petal' not found
> mutate(iris, a = paste( grep("Petal", names(iris), value=T) ))
Error in mutate_impl(.data, dots) : 
  wrong result size (2), expected 150 or 1

And this did not work.

> mutate(iris, a = paste( names(iris)[base::startsWith(names(iris),"Petal")] ))
Error in mutate_impl(.data, dots) : 
  wrong result size (2), expected 150 or 1

I made very troublesome function. But it works. Maybe I use this or search more simple good one.

 > paste.colprefix <-  function(DFNAME, PREFIX){
+     TMP <- eval(parse(text= paste0("grep(\"", PREFIX, "\",names(", DFNAME, "), v=T)")))
+     TMP <- paste0(DFNAME, "$",TMP)
+     TMP <- paste0(TMP, collapse = ",")
+     eval(parse(text= paste0( "paste(", TMP, ")")))
+     }
> 
> iris$PetalPaste <- paste.colprefix("iris", "Petal")  
> head(iris)
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species PetalPaste
1            5.1         3.5          1.4         0.2     setosa    1.4 0.2
2            4.9         3.0          1.4         0.2     setosa    1.4 0.2
3            4.7         3.2          1.3         0.2     setosa    1.3 0.2
4            4.6         3.1          1.5         0.2     setosa    1.5 0.2
5            5.0         3.6          1.4         0.2     setosa    1.4 0.2
6            5.4         3.9          1.7         0.4     setosa    1.7 0.4
> 

1 Answers1

0

You can not use select's helper functions in paste function. Following is the trick with which you can get expected output.
You can filter out column names of the data frame and use them as parameter to your paste function. To filter out those column names you can use any one of the following technique.

  • base::startsWith(character vector, Starts with string)
    cn <- names(iris)[base::startsWith(names(iris),"Petal")]
  • stringr::str_detect(character vector, regex to find)
    cn <- names(iris)[stringr::str_detect(names(iris), "Petal.*")]

In each of this method, it will return vector of column names which start with "Petal".

Then You can use this as following to get your expected result.

iris$a <- do.call(paste,iris[cn])