我正试图用R:中的read.table(file="clipboard")
读取这个伪数据库
Aspecto Sexo Ranking
1 Imagen Hombre 7.50
2 Mantenimiento Hombre 7.18
3 Otro Hombre 7.05
4 Espacios de venta Hombre 6.91
5 Vigilancia Hombre 6.36
6 Tiempo Hombre 6.51
7 Espacios libres Hombre 6.40
8 Imagen Mujer 7.21
9 Mantenimiento Mujer 7.30
10 Otro Mujer 6.90
11 Espacios de venta Mujer 7.02
12 Vigilancia Mujer 6.53
13 Tiempo Mujer 6.40
14 Espacios libres Mujer 5.78
这个代码似乎有效:
pw <- read.table(file="clipboard", dec=".", sep=",", header=TRUE)
但这种结构显然是我不想要的:
str(pw)
'data.frame': 14 obs. of 1 variable:
$ Aspecto...Sexo......Ranking: Factor w/ 14 levels "1
我尝试了很多东西,包括fill=TRUE
和其他论点,但我就是无法得到我所期望的。例如:
pw <- read.table(file="clipboard", dec=".", sep="", header=TRUE)
Error in read.table(file = "clipboard", dec = ".", sep = "", header = TRUE) :
more columns than column names
任何建议都将不胜感激。
您可以使用read.fwf
,因为列有固定的宽度,并且字符串周围没有引号。由于第一行只有3个名称,我们跳过这个,但稍后使用扫描读取它们。
clipboard <- read.fwf("clipboard.txt", widths=c(2,18,9,8), skip=1, as.is=TRUE)
# or row.names=1 to ignore the first un-named column
colnames(clipboard)[2:4] = scan("clipboard.txt", what=rep("character", 3), nlines=1)
str(clipboard)
'data.frame': 14 obs. of 4 variables:
$ V1 : num 1 2 3 4 5 6 7 8 9 10 ...
$ Aspecto: chr " Imagen" " Mantenimiento" " Otro" " Espacios de venta" ...
$ Sexo : chr " Hombre" " Hombre" " Hombre" " Hombre" ...
$ Ranking: num 7.5 7.18 7.05 6.91 6.36 6.51 6.4 7.21 7.3 6.9 ...