Найдите 2 возможных решения, используя dplyr
(и tidyr
для первого решения) ниже:
library(tidyr)
library(dplyr)
df <- read.table(text = " Index State Y2002 Y2003 Y2004 Y2005 Y2006 Y2007 Y2008 Y2009
1 A Alabama 1296530 1317711 1118631 1492583 1107408 1440134 1945229 1944173
2 A Alaska 1170302 1960378 1818085 1447852 1861639 1465841 1551826 1436541
3 A Arizona 1742027 1968140 1377583 1782199 1102568 1109382 1752886 1554330
4 A Arkansas 1485531 1994927 1119299 1947979 1669191 1801213 1188104 1628980
5 C California 1685349 1675807 1889570 1480280 1735069 1812546 1487315 1663809
6 C Colorado 1343824 1878473 1886149 1236697 1871471 1814218 1875146 1752387
", header = T)
# if you want to use group_by, as in your comment,
# then you need to transform data to long first, then calculate, then
# optionally go back to wide:
df %>%
gather(key = year, value = value, matches("\\d{4}$")) %>%
group_by(year) %>%
summarise(value_max = max(value)) %>%
spread(year, value_max)
# if you want to avoid that tranformation, you could use mutate_at:
df %>%
summarise_at(.vars = vars(matches("\\d{4}$")), max)