is there a concept of Shortcuts/Alias/Pointer in R? -
i working on rather large dataset in r split in several dataframes.
the problem things whole set, need work or modify parts of set , selectors getting clunky, f.e.
alistofitems$attribute4([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),] <- alistofitems([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),alistofitems$attribute5] * alistofitems([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),alistofitems$attribute7]
(this sets attribute 4 (attribute5 * attribute6) selected part of entries.)
this horrible read, understand , edit.
splitting these different dataframes not option due ram , because refresh data regulary , rebuilding seperate dataframes pain.
so, there way like
items_t6c <- &(alistofitems([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),]
so can use
items_t6c$attribute4 <- #
alternatively, maybe possible store such selector in string-variable , use it?
you can first construct logical vector, give meaningful name, , use in command. makes script bit longer, easier read:
interesting_bit = with(alistofitems, attribute1 & attribute2 == 6 & attribute3 == "c")
in addition, using bit of indentation makes code more readable.
alistofitems$attribute4[interesting_bit,] <- alistofitems[interesting_bit,alistofitems$attribute5] * alistofitems[interesting_bit,alistofitems$attribute7]
and using within
more readability:
alistofitems[interesting_bit,] = within(alistofitems[interesting_bit,], { attribute4 = attribute5 * attribute7 }
also, logical there no need explicitly test == true
:
interesting_bit = alistofitems$attribute1 & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"
this reduces this:
alistofitems$attribute4([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),] <- alistofitems([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),alistofitems$attribute5] * alistofitems([alistofitems$attribute1 == true & alistofitems$attribute2 == 6 & alistofitems$attribute3 == "c"),alistofitems$attribute7]
to (note additional use of with
):
interesting_bit = with(alistofitems, attribute1 & attribute2 == 6 & attribute3 == "c") alistofitems[interesting_bit,] = within(alistofitems[interesting_bit,], { attribute4 = attribute5 * attribute7 }
this code not less daunting, instantly conveys doing, hard divine original code.
Comments
Post a Comment