Tuesday, October 28, 2014

Reshaping 3 Column R Data Frame to a Square Matrix

Suppose you have an R data frame that looks like this:

Imaginary Flight Time (minutes) 
And you want to create an Origin and Destination matrix or reshape this data frame to a square matrix, then one possible way to do this in R is shown below.

##############################################
## Read data (csv) 
temp <- read.csv("AirportData.csv", stringsAsFactors = TRUE)

## Order temp data in ascending order of origin airport and then destination airport

temp <- temp[order(temp$Origin.Airport, temp$Destination.Airport), ]

## Ordered data looks like this:





## Populating square matrix in R

oriAirport <- temp[, 1]
desAirport <- temp[, 2]
flightTime <- temp[, 3]
Index <- which(desAirport == desAirport[1])
desAirportRange <- Index[2] - 1
oriAirportRange <- length(flightTime) / desAirportRange

distMatrix <- matrix(0, nrow = oriAirportRange, ncol = desAirportRange)
for (i in 1:oriAirportRange){
        for (j in 1:desAirportRange){
                distMatrix[i, j] <- flightTime[(i - 1) * desAirportRange + j]
        }
}

dist <- data.frame(distMatrix)
names(dist) <- c(levels(desAirport))        
row.names(dist) <- c(levels(oriAirport))
View(dist)

##############################################

The resulting square matrix looks like this:







1 comment:


  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete