ここにあなたが試すための小さな例があります:
データを作って読み込んでみる
# 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
ご覧のとおり、read.csv2()
withを使用check.names = FALSE
すると、入力ファイルにある名前をそのまま取得できます。それでは、それを使用して、いくつかのデータを抽出してみましょう。
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'
特定の列を抽出するより効率的な方法
特定の列を抽出するより効率的な方法は、 の別のベクトルを作成し、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