[datatable-help] Efficiency help - create variable conditional on row-specific column values
CCRyder
fluffinator007 at gmail.com
Thu Feb 18 20:51:03 CET 2016
Each row of the table has 15 columns with "ID" and 15 corresponding "ID
Indicator" columns. These are alternating in order (i.e., ID1 / IDind1 /
ID2 / IDind2 /....). The ID Indicator variable specifies whether the
corresponding ID is the "primary ID" for each row/observation (there is only
1 primary ID for each).
My goal is to create a new variable that equals the Primary ID for each row.
I'm doing this now using for loops (see code below). This works, but I am
certain there is a much more efficient way to accomplish the task.
/*RData$PrimaryID <- NA_character_
for(i in seq(9:37, by = 2)) {
for(j in 1:nrow(RData)) {
if(!(is.na(RData[j:i])) & (RData[j,i] == 'Y')) RData[j, 38] <-
NPI[j, (i-1)]
}
}*/
/NOTE: The ID and IDind variables are alternative in positions 9-37 on the
dataframe. The 38th columns is the newly created "PrimaryID"./
I am thinking the better way to do this is with apply/sapply/lapply, or by
vectorizing the work somehow. I haven't been able to get anything working,
though. Any help, tips, or guidance would be very much appreciated!
--
View this message in context: http://r.789695.n4.nabble.com/Efficiency-help-create-variable-conditional-on-row-specific-column-values-tp4717563.html
Sent from the datatable-help mailing list archive at Nabble.com.
More information about the datatable-help
mailing list