我的数据采用以下格式:
input<-data.frame(
region=c("A","T","R","R","T"),
geomorph=c("F","F","S","S","P"),
depth=c(2.6,3.5,5.8,6.7,8.9))
> input
region geomorph depth
1 A F 2.6
2 T F 3.5
3 R S 5.8
4 R S 6.7
5 T P 8.9
我想创建一个汇总表,以便对于给定的深度类别(即0-3,3-6,6-10)区域的条目数(即A,R,T)和地貌(即F,S,P)计数并表示如下:
output<-data.frame(
depth.category=c("0-3","3-6","6-10"),
total=c(1,2,2),
A=c(1,0,0),
R=c(0,1,1),
T=c(0,1,1),
F=c(1,1,0),
S=c(0,1,1),
P=c(0,0,1))
> output
depth.category total A R T F S P
1 0-3 1 1 0 0 1 0 0
2 3-6 2 0 1 1 1 1 0
3 6-10 2 0 1 1 0 1 1
有任何建议如何解决这个问题?
答案 0 :(得分:5)
首先,只需使用cut
创建您的间隔,然后使用table
和cbind
结果:
intervals <- cut(input$depth, breaks=c(0, 3, 6, 10))
cbind(table(intervals),
table(intervals, input$region),
table(intervals, input$geomorph))
# A R T F P S
# (0,3] 1 1 0 0 1 0 0
# (3,6] 2 0 1 1 1 0 1
# (6,10] 2 0 1 1 0 1 1
以上的输出是matrix
。如果您需要data.frame
:
temp <- cbind(table(intervals),
table(intervals, input$region),
table(intervals, input$geomorph))
temp <- data.frame(depth.category = rownames(temp),
as.data.frame(temp, row.names = 1:nrow(temp)))
names(temp)[2] <- "Total"
temp
# depth.category Total A R T F P S
# 1 (0,3] 1 1 0 0 1 0 0
# 2 (3,6] 2 0 1 1 1 0 1
# 3 (6,10] 2 0 1 1 0 1 1