R Experts,
I would like to create a series of variables without having to assign a separate
line of code
for each new variable. My abbreviated dataframe (DF) contains two groups of
linked variables
(SP1:SP3) and (CAU1:CAU3). Within SP1:SP3 are character codes (full dataframe
contains 26
unique codes and SP1:SP9, CAU1:CAU9). CAU1 represents the quantitative
measurement of code x
in variable SP1, and so on through SP3 and CAU3. What I want to do is create a
series of new
variables (one for each unique species code....four in this shortened example)
that contain
the matching CAU variable value for each corresponding species code listed for
each row of the
data set. Example (row 1), the new variable SMB would equal 3 (CAU1) and YP, WE,
and LMB would
contain zero. Example (row 2), the new variable YP would contain 30, and WE
would contain 6,
whereas SMB and LMB would contain zero. If this description is unclear, the
examples below should
illustrate what I what to do.
I have been advised that the reshape package could be useful here but I have
been unsuccessful
at implementing it. I know there has to be a way for this to be done in a few
steps rather than
a separate line of code for each new variable.
Thank you,
Mike
Current method (inefficient): one line per species, example data frame
DF$YP <- with(DF, ifelse(SP1 %in% 'YP', CAU1, ifelse(SP2 %in%
'YP', CAU2,
ifelse(SP3 %in% 'YP', CAU3, 0))))
DF$SMB <- with(DF, ifelse(SP1 %in% 'SMB', CAU1, ifelse(SP2 %in%
'SMB', CAU2,
ifelse(SP3 %in% 'SMB', CAU3, 0))))
DF$LMB <- with(DF, ifelse(SP1 %in% 'LMB', CAU1, ifelse(SP2 %in%
'LMB', CAU2,
ifelse(SP3 %in% 'LMB', CAU3, 0))))
DF$WE <- with(DF, ifelse(SP1 %in% 'WE', CAU1, ifelse(SP2 %in%
'WE', CAU2,
ifelse(SP3 %in% 'WE', CAU3, 0))))
DF <-
structure(list(SP1 = c("SMB", NA, NA, "SMB"), SP2 = c(NA,
"YP",
NA, NA), SP3 = c(NA, "WE", "LMB", NA), CAU1 =
c("3", "0", "0",
"15"), CAU2 = c("0", "30", "0",
"0"), CAU3 = c("0", "6", "5",
"0")), .Names = c("SP1", "SP2", "SP3",
"CAU1", "CAU2", "CAU3"
), row.names = c(NA, -4L), class = "data.frame")