Treatment of text/named columns in cor.test

I have renamed some columns as text in my dataset for ease of interpretation/reading on some figures.

When trying to use them in cor.test (and perhaps other functions?) they seem to require an apparently unnecessary dataframe prefix. Why?

This code throws an error:
with(cs, cor.test('Political orientation', thermo_change_excdflt))

Error in cor.test.default("Political orientation", thermo_change_excdflt) :
'x' and 'y' must have the same length

But this line works perfectly:
with(cs, cor.test(cs$'Political orientation', thermo_change_excdflt))

Pearson's product-moment correlation

data: cs$"Political orientation" and thermo_change_excdflt
t = -0.75971, df = 78, p-value = 0.4497
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
-0.2997761 0.1365847
sample estimates:
cor
-0.08570418

Why is that?

readability makes sense, but snake_case_variable_names are easy to read and need no special quoting.
I think what you need if you must have spaces in variable names are quotes using the backtick symbol `
(underneath it i contrast single quotes, which as you found dont work for this case)

`Political orientation`
'Political orientation'
1 Like

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