以下是您尝试的一个小例子:
# This just creates a CSV in your current working directory to play with
cat("08-03-2013;08-04-2013;08-05-2013\n0,5;0,5;0,5\n0,6;0,6;0,6\n",
file = "FunkyNames.csv")
read.csv2("FunkyNames.csv")
# X08.03.2013 X08.04.2013 X08.05.2013
# 1 0.5 0.5 0.5
# 2 0.6 0.6 0.6
read.csv2("FunkyNames.csv", check.names = FALSE)
# 08-03-2013 08-04-2013 08-05-2013
# 1 0.5 0.5 0.5
# 2 0.6 0.6 0.6
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,使用read.csv2()
with和check.names = FALSE
works来获取输入文件中的名称.现在,让我们使用它并尝试提取一些数据.
temp <- read.csv2("FunkyNames.csv", check.names = FALSE)
## Our first attempt doesn't work
temp$08-03-2013
# Error: unexpected numeric constant in "temp$08"
## Using quotes works
temp$"08-03-2013"
# [1] 0.5 0.6
## The following would work too
## temp$`08-03-2013`
## temp$'08-03-2013'
Run Code Online (Sandbox Code Playgroud)
一个更有效的方法来提取某些列是创建的独立载体names
的data.frame
,使用转换那些日期as.Date
,然后使用矢量从原始到子集data.frame
.一些例子:
tempCols <- as.Date(names(temp), format = "%m-%d-%Y")
tempCols
temp[tempCols > "2013-08-04"]
# 08-05-2013
# 1 0.5
# 2 0.6
temp[tempCols >= "2013-08-04"]
# 08-04-2013 08-05-2013
# 1 0.5 0.5
# 2 0.6 0.6
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
243 次 |
最近记录: |