Relatively new to R and R Studio and I'm trying to reformat a text file to run some analysis on the data within it. I'm currently trying to use read.fwf to tidy the data but seem to be doing something wrong, resulting in various errors. I've attempted to resolve these problems by referring to sources/guides but am still pretty stuck. Any suggestions? (code, examples of the information in the text file, and the desired format are below).
Current Code:
library(readr)
library(tidyr)
read.fwf("AK_JAN_2017_TMAS_", widths =c(1,2,6,1,1,2,2,2,2,2,3,4,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3), header = false, sep = "", row.names = c("Record Type", "FIPS", "Station ID", "Direction of Travel code", "Lane of Travel", "Year of Data", "Month of Data", "Day of Data", "Hour of Data", "Vehicle Class", "Open", "Total Weight of Vehicle", "Number of Axles", "A-axle Weight", "A-B Axle Spacing", "B-axle Weight", "B-C Axle Spacing", "C-axle Weight", "C-D Axle Spacing", "D-axle Spacing", "D-E Axle Spacing", "E-axle Weight", "E-F Axle Spacing", "F-axle Weight", "F-G Axle Spacing", "G-axle Weight", "G-H Axle Spacing", "H-axle Weight", "H-I Axle Spacing", "I-axle Weight", "I-J Axle Spacing", "J-axle Weight", "J-K Axle Spacing", "K-axle Weight", "K-L Axle Spacing", "L-axle Weight", "L-M Axle Spacing", "M-axle Weight"), col.names = NULL, n = -1, buffersize = 2000, fileencoding = "" )
Sample of the text file:
W02000103311701021610061031206057054056013054096054015053015038 W02000103311701021606055024403039038084005121 W02000103311701021609067028505040038054013065104062012064 W02000103311701021705073004302024043019 W02000103311701021710066045606055070075015088094085018086018067 W02000103311701021710080044706052069075015087096083018085018065 W02000103311701021805076007402034056040 W02000103311701021805076004802025043023 W02000103311701021905077002402010051014 W02000103311701021905072004702026042021 W02000103311701021906044020303068053067015068 W02000103311701022006066014803057045049014042 W02000103311701022006053012903058041038014033 W02000103311701022005060003702020043017 W02000103311701022006063009503046047023014026 W02000103311701022105072006602036060030 W02000103311701022206068017703045050059015073 W02000103311701022305065006902033037036 W02000103311701030005066008802032038056 W02000103311701030305066008202037063045
Desired format of the data:
Issues:
library(readr)
library(tidyr)
AK_JAN_2017_TMAS_ <- read.table("~/R Studio Sessions/AK_JAN_2017_TMAS_.txt", quote="\"", comment.char="")
View(AK_JAN_2017_TMAS_)
left<-c(2,4,10,11,12,14,16,18,20,22,25,29,31,34,37,40,43,46,49,52,55,58,61,64,67,70,73,76,79,82,85,88,91,94,97,100,103)
right<-c(3,9,10,11,13,15,17,19,21,24,28,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99,102,105)
df <- data.frame(matrix(numeric(length(x)*length(left)),ncol=length(left)))
Error in numeric(length(x) * length(left)) : object 'x' not found
for (i in 1:length(input.set)) {
stop <- nchar(x[i])
for (j in 1:length(left)) {
df[i,j] <- as.numeric(substr(input.set[i], left[j], right[j]))
if (right[j] == stop) break
}
}
Error in length(input.set) : object 'input.set' not found
df <- data.frame(AK_JAN_2017_TMAS_(numeric(length(x)*length(left)),ncol=length(left)))
Error in AK_JAN_2017_TMAS_(numeric(length(x) * length(left)), ncol = length(left)) :
could not find function "AK_JAN_2017_TMAS_"
for (i in 1:length(input.set)) {
stop <- nchar(x[i])
for (j in 1:length(left)) {
df[i,j] <- as.numeric(substr(input.set[i], left[j], right[j]))
if (right[j] == stop) break
}
}
df <- data.frame(matrix(numeric(length(x)*length(left)),ncol=length(left)))
Error in numeric(length(x) * length(left)) : object 'x' not found
for (i in 1:length('AK_JAN_2017_TMAS_'.set)) {
Error: unexpected symbol in "for (i in 1:length('AK_JAN_2017_TMAS_'.set"
stop <- nchar(x[i])
Error in nchar(x[i]) : object 'x' not found
for (j in 1:length(left)) {
df[i,j] <- as.numeric(substr(input.set[i], left[j], right[j]))
if (right[j] == stop) break
}
Error in substr(input.set[i], left[j], right[j]) :
object 'input.set' not found
}
Error: unexpected '}' in "}"
And a few changes I attempted to make:
df <- data.frame(matrix(numeric(length(x)*length(left)),ncol=length(left)))
for (i in 1:length('AK_JAN_2017_TMAS_'.set)) {
stop <- nchar(x[i])
for (j in 1:length(left)) {
df[i,j] <- as.numeric(substr(input.set[i], left[j], right[j]))
if (right[j] == stop) break
}
}