使用具有nrow的rep函数作为R中不同数据帧的频率

时间:2014-07-24 17:10:46

标签: r apply

如果我有:

One <- c(rep("X",6),rep("Y",3),rep("Z",2))
Two <- c(rep("Blue",4),rep("Red",7))

Three <- c(rep("A",4),rep("B",6),rep("C",1))
Four <- c(rep("Green",5),rep("Black",6))

df1 <- cbind(One,Two)
df2 <- cbind(Three,Four)

df.final <- as.data.frame(rbind(df1,df2))


Month <- c(rep("January",nrow(df1)), rep("February",nrow(df2)))
cbind(Month,df.final)

      Month One   Two
1   January   X  Blue
2   January   X  Blue
3   January   X  Blue
4   January   X  Blue
5   January   X   Red
6   January   X   Red
7   January   Y   Red
8   January   Y   Red
9   January   Y   Red
10  January   Z   Red
11  January   Z   Red
12 February   A Green
13 February   A Green
14 February   A Green
15 February   A Green
16 February   B Green
17 February   B Black
18 February   B Black
19 February   B Black
20 February   B Black
21 February   B Black
22 February   C Black

一切都很花哨,但我的问题是Month <- c(rep("January",nrow(df1)), rep("February",nrow(df2)))我将要有12个数据框,并且想要一个很好的矢量,每个月的频率对应于我拥有的12个数据帧的每个数据帧的小数。直观地说,我认为这样的事情会起作用:

lapply(c(months*),function(x) rep(x,nrow(c(list of 12 dataframes))##pseudo code

我试图避免做Months <- c(rep("Jan",nrow(jan.df)),rep("Feb",nrow(feb.df)),...,rep("Dec",nrow(def.df))) 最后,我将cbind此向量转到所有月份数据帧的rbind。就像在示例中一样,除了那里只有两个月。

1 个答案:

答案 0 :(得分:1)

您是否只想要每个数据框table(df$month)的月份表?这将为您计算每月在数据框中出现的次数。

a <- data.frame(rep("january",10))
names(a) <- c("Month")
b <- a
c <- a
df_list <- list(a,b,c)
sapply(df_list,function(x)table(x$Month))

!> sapply(df_list,function(x)table(x$Month))
 january january january
      10      10      10

如果您想要频率,在整个行中出现的月数,您可以这样做:

sapply(df_list,function(x)table(x$Month)/nrow(x))

!> sapply(df_list,function(x)table(x$Month)/nrow(x))
 january january january
       1       1       1