r - read csv, skip three lines but include header names in data.frame -
this question has answer here:
i want read csv file skip 3 lines (except header) include header names in data.frame. i've tried following header names wrong:
> sine = read.csv(file="sine.csv",head=true,sep=",", skip=3, check.names=true) > colnames(sine) [1] "x0" "x0.0" "x0.0.1" "x0.0.2" "none" "x1.0" "x0.0.3" "none.1" "x.." [10] "x0.1" "x0.2" when read dataset without skipping 3 lines header names ok:
> sine = read.csv(file="sine.csv",head=true,sep=",", skip=0, check.names=true) > colnames(sine) [1] "reset" [2] "angle" [3] "sine" [4] "multisteppredictions.actual" [5] "multisteppredictions.1" [6] "anomalyscore" [7] "multistepbestpredictions.actual" [8] "multistepbestpredictions.1" [9] "anomalylabel" [10] "multistepbestpredictions.multistep.errormetric..altmape..steps..1..window.1000.field.sine" [11] "multistepbestpredictions.multistep.errormetric..aae..steps..1..window.1000.field.sine" what i'm doing wrong?
something this,
foo <- read.csv("http://www.ats.ucla.edu/stat/r/faq/test.csv", header=t) foo # make model mpg weight price # 1 amc concord 22 2930 4099 # 2 amc oacer 17 3350 4749 # 3 amc spirit 22 2640 3799 # 4 buick century 20 3250 4816 # 5 buick electra 15 4080 7827 colnames(foo) # [1] "make" "model" "mpg" "weight" "price" bar <- read.csv("http://www.ats.ucla.edu/stat/r/faq/test.csv", header=t, skip=3) bar # amc spirit x22 x2640 x3799 # 1 buick century 20 3250 4816 # 2 buick electra 15 4080 7827 colnames(bar) # [1] "amc" "spirit" "x22" "x2640" "x3799" as richard scriven pointed out below initial answer did not work, don't know how missed that. found this answer , made solution below.
all_content = readlines("http://www.ats.ucla.edu/stat/r/faq/test.csv") skip_second = all_content[c(c(-2:-4))] foo2 = read.csv(textconnection(skip_second), header = true, stringsasfactors = false) foo2 # make model mpg weight price # 1 buick century 20 3250 4816 # 2 buick electra 15 4080 7827 colnames(foo2) # [1] "make" "model" "mpg" "weight" "price"
Comments
Post a Comment