R使用标记

时间:2016-08-25 18:04:22

标签: r

使用R我想创建一个新列,根据变量的频率标记数据。目前,我有代码删除这些点而不是添加标志列。我需要标记少于7200条记录的数据,这些记录也被另一列标记为yn

我当前的代码会删除这些reocords:

# Read in csv file
data = read.csv(infile)

# Create subset with only records marked 'y'
data_y = subset(data,data$yn!= 'n')

# Count the number of records per day, using table function
valid = table(data_y$DATE)

# Remove the records with less than 7200 records, and also marked 'y' from subset function
df_cut = data_w[data_w$DATE_STAMP %in% names(valid)[valid>=7200],]

# Save new cvs
write.csv(df_cut,outpath)

我假设我不想使用子集,因为这告诉R忽略那些记录但不知道从哪里开始。

1 个答案:

答案 0 :(得分:0)

不确定你要求的确切但......

tmp <- data.frame(table(data$DATE)) 
tmp <- tmp[tmp$Freq < 7200,]

data$flagColumn <- "n"

for(i in 1:nrow(tmp)){
   data$flagColumn[data$DATE == tmp$Freq[i]] <- "y"
}

如果您需要添加多个条件,可以添加&#39;&amp;&#39;

 data$flagColumn[(data$DATE == tmp$Freq[i]]) & data$someOtherCol =="n"] <- "y"