Skip to content
Back to formatted view

Raw Message

Message-ID: <BANLkTi=UN9aM1ATTeRn_nvigOSiAJyihaA@mail.gmail.com>
Date: 2011-05-04T07:25:06Z
From: E Hofstadler
Subject: adding columns to dataframes contained in a list

hi there,

I have a list of 5 identical dataframes:

mydf <- data.frame(x=c(1:5), y=c(21:25))
mylist <- rep(list(mydf),5)

and a factor variable with 5 levels:

foo <- c(letters[1:5])
foo <- as.factor(foo)


Question:
I'd like to add a new variable to each dataframe in the list, each
containing only one level of the factor variable. So mylist[[1]]
should have a new variable z containing only "a", in mylist[[2]]  the
new variable z should contain only "b", etc.

(How) can this be done without looping?

All help is greatly appreciated.

Best,
Esther