Using column / field names with parentheses in R

I am using R version 3.0.2 in Windows 7.

I am loading a CSV table into R, and some of the column names have parentheses such as P (A) or P (A | B). If i try

 whatever<- read.csv("C:/dir/name.csv", header=TRUE);
 hist(whatever$P(A|B));

I get an error

Error: unexpected symbol in "hist(whatever$P(A|B"

Can I use column names with parentheses in R or change the column names to alphanumeric?

+3
source share
2 answers

read.csv()converts special characters to '.' therefore the column "P (A | B)" will be whatever$P.A.B..

However, as @ Floo0 pointed out, if you can have column names like "P (A | B)" that are accessed by whatever$"P(A|B)"or whatever[, "P(A|B)"].

+4
source

Try

hist(whatever$"P(A|B)")

. whatever[,i], - P (A | B)

whatever<-data.frame(test=rnorm(10))
colnames(whatever)<-"P(A|B)"
hist(whatever$"P(A|B)")
+1

All Articles