I have a text data separated ny “commas” i.e.”,”. The sample of the data is given below (first row indicates the column names):
userID,appName,startTime,endTime,endResult
chhieut,gms.mos.test,2012-07-01 02:47:16,2012-07-01 02:47:46,1
chhieut,gms.mos.test,2012-07-01 03:11:46,2012-07-01 03:12:25,2
chhieut,gms.mos.test,2012-07-01 03:13:36,2012-07-01 03:14:03,2
chhieut,gms.mos.test,2012-07-01 03:18:26,2012-07-01 03:18:58,2
chhieut,gms.mos.test,2012-07-01 04:10:36,2012-07-01 04:10:54,2
chhieut,gms.mos.test,2012-07-01 04:38:26,2012-07-01 04:38:48,2
chhieut,gms.mos.test,2012-07-01 04:48:56,2012-07-01 04:49:04,3
chhieut,gms.mos.test,2012-07-01 05:49:46,2012-07-01 05:50:14,2
chhieut,gms.mos.test,2012-07-01 06:19:07,2012-07-01 06:19:25,2
chhieut,gms.mos.test,2012-07-01 07:09:17,2012-07-01 07:09:47,2
I am using the following syntax:
appsession <- read.table("C:/.../AppSession.txt", sep = ",",
col.names = c("userID","appName","startTime","endTime","endResult"),
fill = FALSE, strip.white = TRUE)
I am getting this error:
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 5 elements
Using suitably edited version of your data (i.e. removing all the blank lines!), this can be loaded into R easily via
read.csv(). Note here I’m using a text connection containing the data to avoid writing your data to a file. Just replaceconwith your file name in theread.csv().Also note that by specifying the
colclassesargument we tell R what the data are before reading them in which saves some formatting later, especially with the DateTime data. We can do this here because you have the DateTime variables stored in the correct format.