我想计算每年可变指数中的最高收入。 我的数据框如下:
head(data)
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
Y2010 Y2011 Y2012 Y2013 Y2014 Y2015
1 1237582 1440756 1186741 1852841 1558906 1916661
2 1629616 1230866 1512804 1985302 1580394 1979143
3 1300521 1130709 1907284 1363279 1525866 1647724
4 1669295 1928238 1216675 1591896 1360959 1329341
5 1624509 1639670 1921845 1156536 1388461 1644607
6 1913275 1665877 1491604 1178355 1383978 1330736
答案 0 :(得分:1)
尝试
colMax <- function(data) sapply(data, max, na.rm = TRUE)
colMax(data)
答案 1 :(得分:0)
使用以下dplyr
(第一个解决方案为tidyr
)找到2种可能的解决方案:
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)