How to Deal with Space within variable/column name

Hi There,

I am facing issue while concatenating 2 variable names with having spaces in column name.

I have created first variable name as Geometric mean & second name as CV.

I want to concat both variable name together like "Geometric mean (CV)" within double Quotes to proceed further.

When I tried using "Geometric mean (CV)" & either way around. I am getting below error ;

Error in parse(text = text, keep.source = FALSE) :
:1:11: unexpected symbol
1: Geometric mean

Can you please help me to get variable name as "Geometric mean (CV)"?

Thank you,
Harshal

Use backticks `` instead.

Yes I tried same way within double quotes as below,

"Geometric mean (CV)"

I told you to use backticks instead of double quotes.

Thank you for your suggestion.

As suggested, I tried with backticks but didn't worked out :frowning:

If I used backticks for whole Geometric mean (CV) then R will check for object "Geometric mean (CV)" & will get another error.

My requirement is I want final product as "Geometric mean (CV)" with double quotes.

Both of these work:

`Geometric mean (CV)` <- 1

`"Geometric mean (CV)"` <- 1

As I mentioned in my post, I have 2 separate variables. First variable as "Geometric mean" & second name as "CV".

My requirement is to concat them like "Geometric mean (CV)"

I think we need a FAQ: How to do a minimal reproducible example ( reprex ) for beginners

Thank you!

I know on Reprex however Since I am working on highly regulated environment I can't share live example here :frowning:

Can you mock up one? We almost certainly do not need to see real data. The data layout is what is important.

perhaps this can help you.

library(tidyverse)

(df_1 <- data.frame(a=1,
           b=2))

(df_2 <- mutate(df_1,
         `a b` = a+b))

names(df_2)

df_2$`a b`

Would this work:

df <- data.frame("Geometric mean" = rnorm(10), "CV" = runif(10), check.names = FALSE)
df$`Geometric mean (CV)` <- paste(df$`Geometric mean`, df$CV)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.