back to base

transform e.g. head(airquality) head(transform(airquality, Ozone = -Ozone)) # makes Ozone negative head(transform(airquality, new = -Ozone, Temp = (Temp-32)/1.8)) # creates a new column called 'new' with negative ozone values and changes the values of the Temp column # example of subset one <- subset(bnames, sex == "boy" & year == 2008) one$rank <- rank(-one$percent, ties.method = "first") # or one <- transform(one, rank = rank(-percent, ties.method = "first")) # the use of minus means the rank values go from 1,2,3.. instead of 1000,999,998... # Fine for one year at a time, to do all at once: # Split pieces <- split(bnames, list(bnames$sex, bnames$year)) # Apply results <- vector("list", length(pieces)) for(i in seq_along(pieces)) { piece <- pieces[[i]] piece <- transform(piece, rank = rank(-percent, ties.method = "first")) results[[i]] <- piece } # Combine result <- do.call("rbind", results) # Or equivalently bnames <- ddply(bnames, c("sex", "year"), transform, rank = rank(-percent, ties.method = "first"))

Comments

0 Responses to "back to base"

Post a Comment