简体   繁体   中英

How to read column with column name `0.3` in R?

library(ltm)
library(mokken)
data("LSAT")
df_aisp<-aisp(LSAT,search='normal')
df_aisp<-as.data.frame(df_aisp)
#subset if df_aisp$0.3==1
subset(df_aisp,0.3==1)

Output of above R script is:

[1] 0.3
<0 rows> (or 0-length row.names)

Actually,df_aisp is:

       0.3
Item 1   1
Item 2   0
Item 3   1
Item 4   0
Item 5   0

Which means subset give me wrong result.
How to solve this problem?

We can use backquotes for column names (to evaluate it as it is) that are not in standard format. By standard format, names starting with numbers.

subset(df_aisp,`0.3`==1)

Also, we can specify the column name quoted as such [[ or [ for subsetting

df_aisp[df_aisp[,"0.3"] ==1, , drop = FALSE]

However, we can make the names standard by using make.names which will add a prefix X to the column name

names(df_aisp) <- make.names(names(df_aisp))
names(df_aisp)
#[1] "X0.3"

Then, we don't need the backquotes

 subset(df_aisp, X0.3 == 1)
 #       X0.3
 #Item 1    1
 #Item 3    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM