如何在数据框中查找名称中2个不同年份的不同首字母的频率

时间:2014-10-18 17:00:30

标签: r

df
year   name    sex   frequency  
2010   jack     M      50
2010    jill    F      30
.
.
2011     sam    M      60
2011     zari   F      80

1000行

1 个答案:

答案 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))