r - How to calculate the total value based on multiple column values -
i have data frame contains client names , area data.
i want calculate total area each client areas span on multiple floors (for example, client a may have 202 on floor 1 , 248 on floor 2).
i want create new column total area.
i know how create new column:
areas$new_area and know how calculate total area each client (manually):
sum(areas[areas$client == "client a", "areas"]) what having difficulty iterating through data frame , automating entire process.
i came partial solution iterates through data frame, calculates sum of each area value every client @ position i (which know happen because takes single value in area column, of course):
for(i in 1:nrow(areas)){ areas$new_area[i] <- sum(areas$areas[i]) } also, suspect/know apply function approach take here, don't know 1 use nor how apply (no pun intended).
how can a) achieve , b) achieve in cleaner way?
my expected output (or variation of it):
-------------------------------------- | client | floor | area | new area | -------------------------------------- | | 1 | 202 | 202 | -------------------------------------- | | 2 | 248 | 450 | -------------------------------------- | b | 1 | 1000 | 1000 | -------------------------------------- | b | 2 | 150 | 1150 | -------------------------------------- i want new column @ end total of area values each client (my example shows cumulative total, whether cumulative or not doesn't matter - merely purpose of giving example).
summedareas <- aggregate(area ~ client, areas, sum) allyourdata <- merge(area, summedareas, = "client") i prefer aggregate on tapply because nice data.frame back, calculate totals
tapply(x = areas$area, index = areas$client, fun = sum)
Comments
Post a Comment