在R语言中查找由空格分隔的文本文件的平均值

在R语言中查找由空格分隔的文本文件的平均值,r,R,我在30个不同的目录中有一些名为Reg.stt的文件,其中包含整数和浮点类型的数字数据。我想平均出另一个文件中的数据。我不知道用R语言编码。但根据我的发现,用R语言编写这样的脚本很容易。文件的结构如下所示。。。列和行的数量可能会有所不同 操作系统:Ubuntu 文件夹结构:/Desktop/folder1/Reg.stt、/Desktop/folder2/Reg.stt/桌面/文件夹30/Reg.stt 0 0.3857 0.7942 0.0000 12.418 3.626 4 2 12 4 0

我在30个不同的目录中有一些名为Reg.stt的文件,其中包含整数和浮点类型的数字数据。我想平均出另一个文件中的数据。我不知道用R语言编码。但根据我的发现,用R语言编写这样的脚本很容易。文件的结构如下所示。。。列和行的数量可能会有所不同

操作系统:Ubuntu

文件夹结构:/Desktop/folder1/Reg.stt、/Desktop/folder2/Reg.stt/桌面/文件夹30/Reg.stt

0 0.3857 0.7942 0.0000 12.418 3.626 4 2 12 4 0.3857 0.7942 0.0000 12.418 3.626 4 2 12 4 505 
1 0 0.4269 0.8726 0.0000 11.146 3.730 19 5 8 3 0.4063 0.8726 0.0000 11.782 3.678 19 5 12 4 584 
2 0 0.4427 0.8442 0.0000 11.388 4.014 19 5 15 6 0.4184 0.8726 0.0000 11.651 3.790 19 5 12 4 561 
3 0 0.4472 0.8718 0.0000 11.928 4.134 16 5 23 6 0.4256 0.8726 0.0000 11.720 3.876 19 5 12 4 579 
4 0 0.4511 0.8893 0.0028 11.514 4.176 16 4 31 10 0.4307 0.8893 0.0000 11.679 3.936 16 4 12 4 583 
5 0 0.4546 0.8193 0.0000 11.362 4.204 6 2 6 3 0.4347 0.8893 0.0000 11.626 3.981 16 4 12 4 566 

这里有一种方法。我在代码中做了评论

# Create a sub-folder into which we create files.
dir.create("temp_dir")
setwd("temp_dir")

# Create some files in temp_dir.
sapply(1:10, FUN = function(i) {
  xy <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))
  write.table(xy, file = sprintf("filename_%s.txt", i), row.names = FALSE, col.names = TRUE)
})

# Find relevant files, you can use pattern to match if folder doesn't contain only
# files in question.
all.files <- list.files(pattern = "filename_")

# Read in all files. do not simplify the result because we'll need it "raw". The 
# result is a list.
all.dfs <- sapply(all.files, FUN = read.table, header = TRUE, simplify = FALSE)

# Calculate grand mean of the data.frame.
all.means <- lapply(all.dfs, FUN = function(x) mean(sapply(x, mean)))

# Corce from a list to a vector.
one.mean <- as.numeric(all.means)

# Calculate mean for each column.
mean(one.mean)

# Clean up this example.
setwd("../")
unlink("temp_dir")
#创建一个子文件夹,在其中创建文件。
目录创建(“临时目录”)
setwd(“临时目录”)
#在temp_dir中创建一些文件。
Sappy(1:10,乐趣=功能(i){

xy毫无疑问,也没有可复制的示例,而且您的语句与python无关。基本上,您希望读取每个文件,计算列上的平均值,然后计算所有数据的平均值。帧?@RomanLuštrik是的……如果所有文本文件都有相同的名称并存储在不同的目录中,那又如何呢。例如,directory1/abc.txt……是的ctory2/abc.txt等@AsifKhawaja请参见
?list.files
,特别是参数
recursive=TRUE
。这将“爬升”所有子目录并从中获取文件。如果需要绝对路径名,还可以使用
full.names