如何访问具有特殊字符或 space 的数据 table 列?
How to access a data table column that has special characters or space?
我有以下 data.table:
Date Time User% Sys% Wait% Idle% Busy PhysicalCPUs
1: 01-APR-2015 00:15:28 0.7 0.9 0.1 98.4 NA 64
2: 01-APR-2015 00:30:32 0.7 0.9 0.3 98.1 NA 64
3: 01-APR-2015 00:45:39 0.5 0.7 0.3 98.4 NA 64
4: 01-APR-2015 01:00:46 0.6 0.8 0.3 98.3 NA 64
5: 01-APR-2015 01:15:51 0.5 0.7 0.1 98.6 NA 64
我正在尝试使用以下代码绘制图形:
g1 <- ggplot(CPU_ALL, aes(x = interaction(CPU_AL$Date, CPU_ALL$Time)
, y = CPU_ALL$User%)) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab('Date/Time') + ylab('CPU Utilization (%)') +
ggtitle('CPU ALL')
之后 运行 我收到以下消息:
anobre@segall:nmon$ CPU_ALL.R CPU_ALL_nmon.csv
Error: unexpected input in: "g1 <- ggplot(CPU_ALL, aes(x = interaction(Date, Time)
, y = User%)) +" Execution halted
我认为问题出在我尝试访问列 User%
、Sys%
、Wait%
和 Idle%
时。当我执行 CPU_ALL[,Date]
时,它工作正常,但是当我尝试 CPU_ALL[,User%]
时,我收到以下错误消息:
anobre@segall:nmon$ CPU_ALL.R CPU_ALL_nmon.csv
Error: unexpected input in "CPU_ALL[,User%]"
Execution halted
有谁知道如何访问 data.table 中带有特殊字符或空格的列名?
尝试使用引号:CPU_ALL[,'User%']
如果你的列名有特殊字符,用双引号括起来""
工作脚本
g1 <- ggplot(CPU_ALL, aes(x = interaction(CPU_ALL$Date, CPU_ALL$Time)
, y = CPU_ALL$"User%")) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab('Date/Time') + ylab('CPU Utilization (%)') +
ggtitle('CPU ALL')
如果您使用 read.table
函数和参数 check.names=TRUE
从文件中读取它,顺便说一下,它会自动将您的数据转换为
Date Time User. Sys. Wait. Idle. Busy PhysicalCPUs
1: 01-APR-2015 00:15:28 0.7 0.9 0.1 98.4 NA 64
2: 01-APR-2015 00:30:32 0.7 0.9 0.3 98.1 NA 64
3: 01-APR-2015 00:45:39 0.5 0.7 0.3 98.4 NA 64
4: 01-APR-2015 01:00:46 0.6 0.8 0.3 98.3 NA 64
5: 01-APR-2015 01:15:51 0.5 0.7 0.1 98.6 NA 64
查看 %
符号如何转换为 .
,然后您可以直接访问列而不用双引号引起来。
g1 <- ggplot(CPU_ALL, aes(x = interaction(CPU_ALL$Date, CPU_ALL$Time)
, y = CPU_ALL$User.)) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab('Date/Time') + ylab('CPU Utilization (%)') +
ggtitle('CPU ALL')
希望对您有所帮助。
您可以使用反引号`
符号。
library(data.table)
CPU_ALL=data.table(`User%`=1:2)
CPU_ALL$`User%`
# [1] 1 2
CPU_ALL[,`User%`]
# [1] 1 2
CPU_ALL[,.(`User%`)]
# User%
# 1: 1
# 2: 2
CPU_ALL[`User%` %in% 1:2]
# User%
# 1: 1
# 2: 2
CPU_ALL[["User%"]]
# [1] 1 2
它也适用于 space。
我有以下 data.table:
Date Time User% Sys% Wait% Idle% Busy PhysicalCPUs
1: 01-APR-2015 00:15:28 0.7 0.9 0.1 98.4 NA 64
2: 01-APR-2015 00:30:32 0.7 0.9 0.3 98.1 NA 64
3: 01-APR-2015 00:45:39 0.5 0.7 0.3 98.4 NA 64
4: 01-APR-2015 01:00:46 0.6 0.8 0.3 98.3 NA 64
5: 01-APR-2015 01:15:51 0.5 0.7 0.1 98.6 NA 64
我正在尝试使用以下代码绘制图形:
g1 <- ggplot(CPU_ALL, aes(x = interaction(CPU_AL$Date, CPU_ALL$Time)
, y = CPU_ALL$User%)) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab('Date/Time') + ylab('CPU Utilization (%)') +
ggtitle('CPU ALL')
之后 运行 我收到以下消息:
anobre@segall:nmon$ CPU_ALL.R CPU_ALL_nmon.csv
Error: unexpected input in: "g1 <- ggplot(CPU_ALL, aes(x = interaction(Date, Time)
, y = User%)) +" Execution halted
我认为问题出在我尝试访问列 User%
、Sys%
、Wait%
和 Idle%
时。当我执行 CPU_ALL[,Date]
时,它工作正常,但是当我尝试 CPU_ALL[,User%]
时,我收到以下错误消息:
anobre@segall:nmon$ CPU_ALL.R CPU_ALL_nmon.csv
Error: unexpected input in "CPU_ALL[,User%]"
Execution halted
有谁知道如何访问 data.table 中带有特殊字符或空格的列名?
尝试使用引号:CPU_ALL[,'User%']
如果你的列名有特殊字符,用双引号括起来""
工作脚本
g1 <- ggplot(CPU_ALL, aes(x = interaction(CPU_ALL$Date, CPU_ALL$Time)
, y = CPU_ALL$"User%")) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab('Date/Time') + ylab('CPU Utilization (%)') +
ggtitle('CPU ALL')
如果您使用 read.table
函数和参数 check.names=TRUE
从文件中读取它,顺便说一下,它会自动将您的数据转换为
Date Time User. Sys. Wait. Idle. Busy PhysicalCPUs
1: 01-APR-2015 00:15:28 0.7 0.9 0.1 98.4 NA 64
2: 01-APR-2015 00:30:32 0.7 0.9 0.3 98.1 NA 64
3: 01-APR-2015 00:45:39 0.5 0.7 0.3 98.4 NA 64
4: 01-APR-2015 01:00:46 0.6 0.8 0.3 98.3 NA 64
5: 01-APR-2015 01:15:51 0.5 0.7 0.1 98.6 NA 64
查看 %
符号如何转换为 .
,然后您可以直接访问列而不用双引号引起来。
g1 <- ggplot(CPU_ALL, aes(x = interaction(CPU_ALL$Date, CPU_ALL$Time)
, y = CPU_ALL$User.)) +
geom_line() +
geom_point() +
expand_limits(y=0) +
xlab('Date/Time') + ylab('CPU Utilization (%)') +
ggtitle('CPU ALL')
希望对您有所帮助。
您可以使用反引号`
符号。
library(data.table)
CPU_ALL=data.table(`User%`=1:2)
CPU_ALL$`User%`
# [1] 1 2
CPU_ALL[,`User%`]
# [1] 1 2
CPU_ALL[,.(`User%`)]
# User%
# 1: 1
# 2: 2
CPU_ALL[`User%` %in% 1:2]
# User%
# 1: 1
# 2: 2
CPU_ALL[["User%"]]
# [1] 1 2
它也适用于 space。