-1

我很想在 R 中使用“if”函数创建新向量,以仅提取新数组的某些值。基本上,我想为几个城市中的每一个按星期几分隔数据。如何使用 apply 函数仅获取每个城市的新数组中的星期二?谢谢

4

2 回答 2

3

It sounds as though you don't want if or apply at all. The solution is simpler:

Suppose that your data frame is data. Then subset(data, Weekday == 3) should work.

于 2012-07-31T21:31:01.497 回答
2

You don't want to use the R if. Instead use the subsetting function [

dat <- read.table(text="    Date    Weekday Holiday Atlanta Chicago Houston Tulsa
1   1/1/2008    3   1   313 313 361 123
2   1/2/2008    4   0   735 979 986 310
3   1/3/2008    5   0   690 904 950 286
4   1/4/2008    6   0   610 734 822 281
5   1/5/2008    7   0   482 633 622 211
6   1/6/2008    1   0   349 421 402 109", header=TRUE)
dat[ dat$Weekday==3, ]
于 2012-07-31T21:29:56.007 回答