如何在R中对数据帧列表进行子集化?

时间:2013-07-31 12:43:22

标签: r list dataframe subset

我有多个物理变量数据集,我想用R做一些工作。但是,我想使用一个列表。这是我的1个数据帧的代码:

# Table definition
df.jannuary <- read.table("C:\\...file1.csv", sep=";")

# Subset of the table containing only variables of interest
df.jannuary_sub <- subset(df.jannuary, select=c(2:8, 11:12))

# Column names
colnames(df.jannuary_sub)<-c("year","day","hour","minute","temp_air","temp_eau","humidity_rel","wind_intensity","wind_direction")

# Aggregation of the 4 Year-Day-Hour-Minute columns into a single column and conversion into a POSIXct objet through the temporary column "timestamp"
df.jannuary_sub$timestamp <- as.POSIXct(paste(df.jannuary_sub$year, df.jannuary_sub$day, df.jannuary_sub$hour, df.jannuary_sub$minute), format="%Y %j %H %M", tz="GMT")

# Getting the date with a new format from julian day to normal day into a column called "date"
df.jannuary_sub$date <- format(df.jannuary_sub$timestamp,"%d/%m/%Y %H:%M",tz = "GMT")

# Suppression of the 4 Year-Day-Hour-Minute initial columns and of the temporary column "timestamp", and placement of the date column as column 1
df.jannuary_sub <- subset(df.jannuary_sub, select=c(11, 5:9))

此代码有效。事情是我一年中的所有月份都持续了好几年。

所以我开始使用列表,这是2011年的例子:

df.jannuary <- read.table("C:\\...\file1.dat", sep=",")
#...
df.december <- read.table("C:\\...\file12.dat", sep=",")

# Creation of a list containing the month datasets, with a subset of the tables containing only variables of interest
list.dataset_2011<-list(
df.jannuary_sub <- subset(df.jannuary, select=c(2:8, 11:12)),
#...
df.december_sub <- subset(df.december, select=c(2:8, 11:12))
)

# Column names for all variables of the list for (j in 1:12)
{
colnames(list.dataset_2011[[j]])<-c("year","day","hour","minute","temp_air","temp_eau","humidity_rel","wind_intensity","wind_direction")
}

# Conversion of the list into a data.frame called "list.dataset_2011" for (i in 1:9)
{
list.dataset_2011[[i]]<-as.data.frame(list.dataset_2011[[i]])
}

# Aggregation of the 4 Year-Day-Hour-Minute columns into a single column and conversion into a POSIXct objet through the temporary column "timestamp"
list.dataset_2011$timestamp <- as.POSIXct(paste(list.dataset_2011$year, list.dataset_2011$day, list.dataset_2011$hour, list.dataset_2011$minute), format="%Y %j %H %M", tz="GMT")

# Getting the date with a new format from julian day to normal day into a column called "date"
list.dataset_2011$date <- format(list.dataset_2011$timestamp,"%d/%m/%Y %H:%M",tz = "GMT")

# Suppression of the 4 Year-Day-Hour-Minute initial columns and of the temporary column "timestamp", and placement of the date column as column 1
list.dataset_2011 <- subset(list.dataset_2011, select=c(11, 5:9))

我在代码的末尾遇到了一个问题(希望其余的工作正常!),使用subset命令,它似乎不适用于属性“list”。

0 个答案:

没有答案
相关问题