Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 智能电子表格解析(管理组子标题和总和行等)_R - Fatal编程技术网

R 智能电子表格解析(管理组子标题和总和行等)

R 智能电子表格解析(管理组子标题和总和行等),r,R,假设您有一套格式如下的电子表格: 是否有一个已建立的方法/库可以将其解析为R,而无需单独编辑源电子表格?其目的是解析标题行并省去求和行,以便输出为原始数据,如下所示: Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0

假设您有一套格式如下的电子表格:

是否有一个已建立的方法/库可以将其解析为R,而无需单独编辑源电子表格?其目的是解析标题行并省去求和行,以便输出为原始数据,如下所示:

    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            7.0         3.2          4.7         1.4 versicolor
5            6.4         3.2          4.5         1.5 versicolor
6            6.9         3.1          4.9         1.5 versicolor
7            5.7         2.8          4.1         1.3 versicolor
8            6.3         3.3          6.0         2.5  virginica
9            5.8         2.7          5.1         1.9  virginica
10           7.1         3.0          5.9         2.1  virginica
我当然可以找到一个专门针对这个问题的解决方案,但我想知道还有什么比
read.csv
和大量的逻辑更先进/优雅的东西

这是一个可复制的演示csv数据集(不能假设每组的行数相等),尽管我希望解决方案可以转换为
*.xlsx

,Sepal.Length,Sepal.Width,Petal.Length,Petal.Width
,,,,
Setosa,,,,
1,5.1,3.5,1.4,0.2
2,4.9,3,1.4,0.2
3,4.7,3.2,1.3,0.2
Mean,4.9,3.23,1.37,0.2
,,,,
Versicolor,,,,
1,7,3.2,4.7,1.4
2,6.4,3.2,4.5,1.5
3,6.9,3.1,4.9,1.5
Mean,6.77,3.17,4.7,1.47
,,,,
Virginica,,,,
1,6.3,3.3,6,2.5
2,5.8,2.7,5.1,1.9
3,7.1,3,5.9,2.1
Mean,6.4,3,5.67,2.17

呈现电子表格的方式多种多样,因此很难为所有演示提供一致的方法。但是,在R中加载数据后,可以对其进行转换。下面是一个数据示例。它使用package
zoo
中的函数
na.locf

x <- read.csv(text=",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width
,,,,
Setosa,,,,
1,5.1,3.5,1.4,0.2
2,4.9,3,1.4,0.2
3,4.7,3.2,1.3,0.2
Mean,4.9,3.23,1.37,0.2
,,,,
Versicolor,,,,
1,7,3.2,4.7,1.4
2,6.4,3.2,4.5,1.5
3,6.9,3.1,4.9,1.5
Mean,6.77,3.17,4.7,1.47
,,,,
Virginica,,,,
1,6.3,3.3,6,2.5
2,5.8,2.7,5.1,1.9
3,7.1,3,5.9,2.1
Mean,6.4,3,5.67,2.17", header=TRUE, stringsAsFactors=FALSE)

library(zoo)
x <- x[x$X!="Mean",] #remove Mean line
x$Species <- x$X     #create species column
x$Species[grepl("[0-9]",x$Species)] <- NA  #put NA if Species contains numbers
x$Species <- na.locf(x$Species)             #carry last observation if NA
x <- x[!rowSums(is.na(x))>0,]               #remove lines with NA

   X Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
3  1          5.1         3.5          1.4         0.2     Setosa
4  2          4.9         3.0          1.4         0.2     Setosa
5  3          4.7         3.2          1.3         0.2     Setosa
9  1          7.0         3.2          4.7         1.4 Versicolor
10 2          6.4         3.2          4.5         1.5 Versicolor
11 3          6.9         3.1          4.9         1.5 Versicolor
15 1          6.3         3.3          6.0         2.5  Virginica
16 2          5.8         2.7          5.1         1.9  Virginica
17 3          7.1         3.0          5.9         2.1  Virginica

x呈现电子表格的方式多种多样,因此很难为所有演示提供一致的方法。但是,在R中加载数据后,可以对其进行转换。下面是一个数据示例。它使用package
zoo
中的函数
na.locf

x <- read.csv(text=",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width
,,,,
Setosa,,,,
1,5.1,3.5,1.4,0.2
2,4.9,3,1.4,0.2
3,4.7,3.2,1.3,0.2
Mean,4.9,3.23,1.37,0.2
,,,,
Versicolor,,,,
1,7,3.2,4.7,1.4
2,6.4,3.2,4.5,1.5
3,6.9,3.1,4.9,1.5
Mean,6.77,3.17,4.7,1.47
,,,,
Virginica,,,,
1,6.3,3.3,6,2.5
2,5.8,2.7,5.1,1.9
3,7.1,3,5.9,2.1
Mean,6.4,3,5.67,2.17", header=TRUE, stringsAsFactors=FALSE)

library(zoo)
x <- x[x$X!="Mean",] #remove Mean line
x$Species <- x$X     #create species column
x$Species[grepl("[0-9]",x$Species)] <- NA  #put NA if Species contains numbers
x$Species <- na.locf(x$Species)             #carry last observation if NA
x <- x[!rowSums(is.na(x))>0,]               #remove lines with NA

   X Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
3  1          5.1         3.5          1.4         0.2     Setosa
4  2          4.9         3.0          1.4         0.2     Setosa
5  3          4.7         3.2          1.3         0.2     Setosa
9  1          7.0         3.2          4.7         1.4 Versicolor
10 2          6.4         3.2          4.5         1.5 Versicolor
11 3          6.9         3.1          4.9         1.5 Versicolor
15 1          6.3         3.3          6.0         2.5  Virginica
16 2          5.8         2.7          5.1         1.9  Virginica
17 3          7.1         3.0          5.9         2.1  Virginica

x我最近也做了类似的事情。以下是我的解决方案:

iris <- read.csv(text=",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width
,,,,
Setosa,,,,
1,5.1,3.5,1.4,0.2
2,4.9,3,1.4,0.2
3,4.7,3.2,1.3,0.2
Mean,4.9,3.23,1.37,0.2
,,,,
Versicolor,,,,
1,7,3.2,4.7,1.4
2,6.4,3.2,4.5,1.5
3,6.9,3.1,4.9,1.5
Mean,6.77,3.17,4.7,1.47
,,,,
Virginica,,,,
1,6.3,3.3,6,2.5
2,5.8,2.7,5.1,1.9
3,7.1,3,5.9,2.1
Mean,6.4,3,5.67,2.17", header=TRUE, stringsAsFactors=FALSE)

iris我最近也做了类似的事情。以下是我的解决方案:

iris <- read.csv(text=",Sepal.Length,Sepal.Width,Petal.Length,Petal.Width
,,,,
Setosa,,,,
1,5.1,3.5,1.4,0.2
2,4.9,3,1.4,0.2
3,4.7,3.2,1.3,0.2
Mean,4.9,3.23,1.37,0.2
,,,,
Versicolor,,,,
1,7,3.2,4.7,1.4
2,6.4,3.2,4.5,1.5
3,6.9,3.1,4.9,1.5
Mean,6.77,3.17,4.7,1.47
,,,,
Virginica,,,,
1,6.3,3.3,6,2.5
2,5.8,2.7,5.1,1.9
3,7.1,3,5.9,2.1
Mean,6.4,3,5.67,2.17", header=TRUE, stringsAsFactors=FALSE)

iris好的,我很喜欢
na.locf
。否则,这仍然是“read.csv和一堆逻辑”,但无可否认相当优雅。谢谢。好的,我喜欢
na.locf
。否则,这仍然是“read.csv和一堆逻辑”,但无可否认相当优雅。谢谢。我认为这里有一些Hadleyverse的漏洞-即“分析电子表格的语法”来补充dplyr..我认为这里有一些Hadleyverse的漏洞-即“分析电子表格的语法”来补充dplyr。。
split_at(iris, index) %>% 
.[2:length(.)] %>% 
purrr::map_df(function(x) {
  Species <- x[1,1]
  x <- x[-c(1,NROW(x) - 1, NROW(x)),]
  data.frame(x, Species = Species)
})