df
year name sex frequency
2010 jack M 50
2010 jill F 30
.
.
2011 sam M 60
2011 zari F 80
1000行
答案 0 :(得分:2)
你可以尝试:
res <- as.data.frame.matrix(with(df, table(year, substr(name,1,1))))
res
# j s z
#2010 2 0 0
#2011 0 1 1
df <- structure(list(year = c(2010L, 2010L, 2011L, 2011L), name = c("jack",
"jill", "sam", "zari"), sex = c("M", "F", "M", "F"), frequency = c(50L,
30L, 60L, 80L)), .Names = c("year", "name", "sex", "frequency"
), class = "data.frame", row.names = c(NA, -4L))