[R] How to apply functions across columns?

Robert Latest boblatest at gmail.com
Wed May 9 16:12:17 CEST 2012


Hello,

me again.

I have a data frame that looks like this (actual dput output at bottom):

> head(tencor)
        date    lot wf.id   s1   s2   s3   s4   s5
1 08.05.2012 W0X3H0     9 1238 1263 1244 1200 1183
2 08.05.2012 W0X3H0    10 1367 1396 1371 1325 1311
3 08.05.2012 W0X3H0    11 1383 1417 1393 1346 1328

I'd like to add a column to this that gives, for each row, the
averages of the values in the columns s1 to s5. Really primitive. But
I totally absolute don't understand how to do this. I don't need any
"intelligence", I know my values are always in columns 4:8.

Thanks,
robert

> dput(tencor)
structure(list(date = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "08.05.2012", class = "factor"), lot =
structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "W0X3H0", class = "factor"),
    wf.id = c(9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L,
    4L), s1 = c(1238L, 1367L, 1383L, 1395L, 1479L, 1411L, 1404L,
    1398L, 1402L, 1380L, 1376L), s2 = c(1263L, 1396L, 1417L,
    1420L, 1527L, 1452L, 1438L, 1432L, 1432L, 1412L, 1403L),
    s3 = c(1244L, 1371L, 1393L, 1395L, 1497L, 1424L, 1410L, 1404L,
    1398L, 1382L, 1385L), s4 = c(1200L, 1325L, 1346L, 1346L,
    1444L, 1372L, 1361L, 1362L, 1359L, 1338L, 1334L), s5 = c(1183L,
    1311L, 1328L, 1336L, 1426L, 1357L, 1347L, 1344L, 1339L, 1325L,
    1322L)), .Names = c("date", "lot", "wf.id", "s1", "s2", "s3",
"s4", "s5"), class = "data.frame", row.names = c(NA, -11L))
>



More information about the R-help mailing list