【发布时间】:2016-09-23 17:50:13
【问题描述】:
我按照 Hadley 的线程:Issue in Loading multiple .csv files into single dataframe in R using rbind 读取多个 CSV 文件,然后将它们转换为一个数据帧。我还试验了lapply 与sapply,如Grouping functions (tapply, by, aggregate) and the *apply family 所述。
这是我的第一个 CSV 文件:
dput(File1)
structure(list(First.Name = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("A",
"C"), class = "factor"), Last.Name = structure(c(1L, 2L, 2L,
2L, 2L), .Label = c("B", "D"), class = "factor"), Income = c(55L,
23L, 34L, 45L, 44L), Tax = c(23L, 21L, 22L, 24L, 25L), Location = structure(c(3L,
3L, 1L, 4L, 2L), .Label = c("Americas", "AP", "EMEA", "LATAM"
), class = "factor")), .Names = c("First.Name", "Last.Name",
"Income", "Tax", "Location"), class = "data.frame", row.names = c(NA,
-5L))
这是我的第二个 CSV 文件:
dput(File2)
structure(list(First.Name = structure(c(1L, 2L, 1L, 1L, 1L), .Label = c("A",
"C"), class = "factor"), Last.Name = structure(c(1L, 2L, 2L,
2L, 2L), .Label = c("B", "D"), class = "factor"), Income = c(55L,
55L, 55L, 55L, 55L), Tax = c(24L, 24L, 24L, 24L, 24L), Location = structure(c(3L,
3L, 1L, 4L, 2L), .Label = c("Americas", "AP", "EMEA", "LATAM"
), class = "factor")), .Names = c("First.Name", "Last.Name",
"Income", "Tax", "Location"), class = "data.frame", row.names = c(NA,
-5L))
这是我的代码:
dat1 <-",First.Name,Last.Name,Income,Tax,Location\n1,A,B,55,23,EMEA\n2,C,D,23,21,EMEA\n3,A,D,34,22,Americas\n4,A,D,45,24,LATAM\n5,A,D,44,25,AP"
dat2 <-",First.Name,Last.Name,Income,Tax,Location\n1,A,B,55,24,EMEA\n2,C,D,55,24,EMEA\n3,A,D,55,24,Americas\n4,A,D,55,24,LATAM\n5,A,D,55,24,AP"
tc1 <- textConnection(dat1)
tc2 <- textConnection(dat2)
merged_file <- do.call(rbind, lapply(list(tc1,tc2), read.csv))
虽然效果很好,但我想将 lapply 更改为 sapply。从上面的线程中,我意识到sapply 会将读取因子从csv 文件更改为矩阵,但我不确定为什么会翻转这些字段。例如,Income 字段占用第 3 行和第 8 行,但不在一列中。
代码如下:
tc1 <- textConnection(dat1)
tc2 <- textConnection(dat2)
# change lapply to sapply
merged_file <- do.call(rbind, sapply(list(tc1,tc2), read.csv))
这是输出:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 1 1 1
[2,] 1 2 2 2 2
[3,] 55 23 34 45 44
[4,] 23 21 22 24 25
[5,] 3 3 1 4 2
[6,] 1 2 1 1 1
[7,] 1 2 2 2 2
[8,] 55 55 55 55 55
[9,] 24 24 24 24 24
[10,] 3 3 1 4 2
如果有任何帮助,我将不胜感激。我对 R 相当陌生,不确定发生了什么。
【问题讨论】:
-
为什么要把
lapply改成sapply?lapply是这里合适的函数,效率更高。顺便说一句,paste是矢量化的。 -
@RichScriven - 我只是在尝试了解使用
sapply而不是lapply时输出不同的原因。 -
“虽然这很好用” 作为一个可重现的例子,它甚至根本不起作用。我们没有你的路径,所以它会失败。从
textConnection()而不是文件中读取数据帧是最简单的。我编辑了你的代码。 -
这个问题与因素无关,它是通用的 sapply 与 lapply。与Why does sapply return a matrix that I need to transpose... 重复