In R: Selecting and replacing a sequence into a new column -
i have .csv looks @ waste generation in us, loaded in r studio. creating path waste shipments shipper receiver. tried visualizing data below give sense of need. describe it, create new column called "path" takes second value of data$receiver state, , lays below first value of data$shipper state. repeat every series of 2 rows.
so far, have tried selecting sequence of data$shipper, , odd sequence of data$receiver, not know how overlay them data$path.
currently, data looks this
shipper | receiver ---------------------- kansas | wyoming kansas | wyoming texas | vermont texas | vermont idaho | ohio idaho | ohio
and this:
shipper | receiver | path ---------------------------- kansas | wyoming | kansas kansas | wyoming | wyoming texas | vermont | texas texas | vermont | vermont idaho | ohio | idaho idaho | ohio | ohio
thank help!
i sure there other simpler ways this. use basic loops. please see if below code works you:
for(i in 1:nrow(data)) { if(i %% 2 == 0) { data$path[i] <- as.character(data$receiver[i]) } else { data$path[i] <- as.character(data$shipper[i]) } }
Comments
Post a Comment