rename_
works as expected for non-pathological column names
%>% rename_(foo = 'testcol')
But what if I'd like to rename a column which has a space?
%>% rename_(foo = 'test col')
I get an error that says:
Error in parse(text = x) (from #12) : <text>:1:6: unexpected symbol
I could use make.names
but is there no way to rename a column without that extra step?
rename() function from dplyr takes a syntax rename(new_column_name = old_column_name) to change the column from old to a new name. The following example renames the column from id to c1 . The operator – %>% is used to load the renamed column names to the data frame.
To rename a column in R, you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B” again, you can run the following code: rename(dataframe, B = A) .
To change multiple column names by name and by index use rename() function of the dplyr package and to rename by just name use setnames() from data. table . From R base functionality, we have colnames() and names() functions that can be used to rename a data frame column by a single index or name.
rename() function in R Language is used to rename the column names of a data frame, based on the older names.
You can try with backquotes
%>% rename(foo = `test col`)
Using a reproducible example
library(dplyr)
df %>%
rename(foo = `test col`) %>%
head(3)
# Col1 foo
#1 -0.5458808 C
#2 0.5365853 N
#3 0.4196231 R
Or using rename_
(though I am not sure if this is correct syntax as usually .dots
are needed.) Using similar syntax from OP's post
df %>%
rename_(foo = quote(`test col`)) %>%
head(3)
# Col1 foo
#1 -0.5458808 C
#2 0.5365853 N
#3 0.4196231 R
set.seed(24)
df <- data.frame(Col1= rnorm(10), 'test col' = sample(LETTERS, 10),
check.names=FALSE)
Here is the underlying reason for this behavior. To fix the problem, @akrun's answer is probably more suitable.
Most dplyr
functions use lazyeval
internally. And the character method for lazyeval::as.lazy
cannot handle spaces. A possible fix would be to add the around character strings with spaces inside
as.lazy.character`.
require(lazyeval)
as.lazy.character <- function (x, env = baseenv()){
if (grepl(pattern = "[:space:]", x) & !grepl(pattern = "`", x))
x <- paste0("`", x, "`")
lazy_(parse(text = x)[[1]], env)
}
Or better yet (from @hadley's suggestion)
as.lazy.character <- function (x, env = baseenv()){
if (grepl(pattern = "[:space:]", x) & !grepl(pattern = "`", x))
return(as.lazy(as.name(x), env))
lazy_(parse(text = x)[[1]], env)
}
That fixes the rename_
, as well as any other functions using as.lazy
internally:
dplyr::select_vars_(names(df), "test col")
dplyr::rename_(df, foo="test col")
dplyr::mutate_(df, "foo" = "test col" )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With