-1
data='
编号,性别,序号,民族,籍贯,系部
1,男,20130990,壮族,广西省,旅游系
24,男,20130520,汉族,广东省,经管系
25,男,20130521,汉族,海南省,经管系
26,男,20130522,汉族,海南省,经管系
27,男,20130523,汉族,海南省,经管系
28,男,20130524,汉族,浙江,经管系
29,男,20130525,汉族,浙江,经管系
30,女,20130526,汉族,海南,经管系
31,女,20130527,汉族,江西省,经管系
32,男,20130528,汉族,浙江,经管系
33,男,20130529,汉族,海南省,经管系
34,男,20130530,黎族,海南省,经管系
48,女,20131065,汉族,河北省,艺术系,,
49,女,20131066,汉族,湖南省,艺术系,,
50,女,20131067,汉族,广东省,艺术系,,
51,男,20131068,汉族,海南省,艺术系,,
52,男,20131069,汉族,海南省,艺术系,,
53,男,20131070,汉族,重庆市,艺术系,,
127,男,20130338,汉族,江西省,经管系
128,男,20130339,汉族,海南省,经管系
129,男,20130340,汉族,海南省,经管系
130,男,20130341,汉族,四川省,经管系
131,男,20130342,汉族,四川省,经管系'

how to read the data with read.table in R ? There are 6 columns in some lines ;7 columns in some lines(6 colunmes +1 blank column),these lines end with two commas.

showkey
  • 482
  • 42
  • 140
  • 295

1 Answers1

0

I do not have asian fonts installed hence using the below sample text:

a,b,c,d,e,f
x,2,3,4,5,6
y,6,5,4,3,2,1

Here the columns are unequal with 6,6, and 7 in rows 1,2,3 including the header.

From this (Import data into R with an unknown number of columns?)

saving the contents to somefile.csv

no_col <- max(count.fields("somefile.csv", sep = ","))
data <- read.csv('somefile.csv',fill=TRUE,col.names=1:no_col)
>data
  X1 X2 X3 X4 X5 X6 X7
1  x  2  3  4  5  6 NA
2  y  6  5  4  3  2  1

Here count.fields counts the number of columns and the col.names are generated based on number of columns

You could change the column header using,

colnames(data)<- namevector

where namevector is the vector containing the column names you require

Community
  • 1
  • 1
Silence Dogood
  • 3,587
  • 1
  • 13
  • 17