[R] How to apply functions across columns?

R. Michael Weylandt michael.weylandt at gmail.com
Wed May 9 16:19:05 CEST 2012


Good reproducible example ;-)

Easiest is probably just:

cbind(tencor, ThisRowMean =  rowMeans(tencor[, 4:8]))

# Whatever you replace "ThisRowMean" with will become the column name

Best,
Michael

On Wed, May 9, 2012 at 10:12 AM, Robert Latest <boblatest at gmail.com> wrote:
> 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))
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list