数据帧名称R.

Hen*_*nri 1 excel r date dataframe

我通过该read.cv2()函数导入R中的.csv文件(来自Excel 2010).

我得到了dataframe.我的专栏名称应该是日期,但我得到类似的东西X08.03.2013.

我有几个问题:

  1. 如何将这些名称设置为日期格式(行名称的同义词)?
  2. 对于列,一旦我得到日期格式,我如何在这些日期使用条件(if)?

我希望我已经足够清楚了.谢谢您的帮助.

A5C*_*2T1 5

以下是您尝试的一个小例子:

弥补一些数据并尝试阅读

# 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 = FALSEworks来获取输入文件中的名称.现在,让我们使用它并尝试提取一些数据.

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)

提取某些列的更有效方法

一个更有效的方法来提取某些列是创建的独立载体namesdata.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)