对多个文本文件进行计算并从中生成数据帧R

对多个文本文件进行计算并从中生成数据帧R,r,dataframe,R,Dataframe,我正试图从我对几个文本文件所做的计算中创建一个表。我想这可能需要某种循环,但我一直在思考如何继续。我尝试过不同的循环,但似乎没有一个有效。我已经设法用一个文件做了我想做的事情。这是我的工作代码: flare <- read.table("C:/temp/HD3_Bld_CD8_TEM.txt", header=T) head(flare[,c(1,2)]) #sum of the freq column, check to see if close t

我正试图从我对几个文本文件所做的计算中创建一个表。我想这可能需要某种循环,但我一直在思考如何继续。我尝试过不同的循环,但似乎没有一个有效。我已经设法用一个文件做了我想做的事情。这是我的工作代码:

flare <- read.table("C:/temp/HD3_Bld_CD8_TEM.txt", 
                header=T)

head(flare[,c(1,2)])

#sum of the freq column, check to see if close to 1
sum(flare$freq)

#Sum of top 10
ten <- sum(flare$freq[1:10])
#Sum of 11-100
to100 <- sum(flare$freq[11:100])
#Sum of 101-1000
to1000 <- sum(flare$freq[101:1000])
#sum of 1001+
rest <- sum(flare$freq[-c(1:1000)])

#place the values of the sum in a table
df <- data.frame(matrix(ncol = 1, nrow = 4))
x <- c("Sum")
colnames(df) <- x
y <- c("10", "11-100", "101-1000", "1000+")
row.names(df) <- y
df[,1] <- c(ten,to100,to1000,rest)
这非常适合制作堆叠条形图,我就是这么做的。但是,这仅适用于一个文本文件。我有几个相同的文件。它们都有相同的列名,所以我知道它们都将使用DF$freq列进行计算。在对每个文件进行计算后,如何制作表格?我想保留文本文件的名称作为示例名称,这样当我制作一个联合堆叠条形图时,所有的名称都会在那里。另外,在编写新表/数据框时,确定数据方向的最佳方法是什么


我对R还是新手,所以任何帮助,任何解释都将是最受欢迎的。谢谢。

像这样的事情怎么样,你的例子是不可复制的,所以我做了一个虚拟的例子,你可以调整:

library(tidyverse)
###load ALL your dataframes
test_df_1 <- data.frame(var1 = matrix(c(1,2,3,4,5,6), nrow = 6, ncol = 1))
test_df_1
test_df_2 <- data.frame(var2 = matrix(c(7,8,9,10,11,12), nrow = 6, ncol = 1))
test_df_2
### Bind them into one big wide dataframe
df <- cbind(test_df_1, test_df_2)
### Add an id column which repeats (in your case adjust this to repeat for the grouping you want, i.e replace the each = 2 with each = 10, and each = 4 with each = 100)
df <- df %>% 
  mutate(id = paste0("id_", c(rep(1, each = 2), rep(2, each = 4))))
### Gather your dataframes into long format by the id
df_gathered <- df %>% 
  gather(value = value, key = key, - id)
df_gathered
### use group_by to group data by id and summarise to get the sum of each group
df_gathered_sum <- df_gathered %>% 
  group_by(id, key) %>% 
  summarise(sigma = sum(value))
df_gathered_sum
库(tidyverse)
###加载所有数据帧

我想我解决了!它给了我想要的数据框,从中,我可以制作堆叠的条形图来显示数据

sumfunction <- function(x) {
wow <- read.table(x, header=T)
#Sum of top 10
ten <- sum(wow$freq[1:10])
#Sum of 11-100
to100 <- sum(wow$freq[11:100])
#Sum of 101-1000
to1000 <- sum(wow$freq[101:1000])
#sum of 1001+
rest <- sum(wow$freq[-c(1:1000)])
blah <- c(ten,to100,to1000,rest)
}

library(data.table)
library(tools)
dir = "C:/temp/"
filenames <- list.files(path = dir, pattern = "*.txt", full.names = FALSE)
alltogether <- lapply(filenames, function(x) sumfunction(x))
data <- as.data.frame(data.table::transpose(alltogether), 
                  col.names =c("Top 10 ", "From 11 to 100", "From 101 to 1000", "From 1000 on "),
                  row.names = file_path_sans_ext(basename(filenames)))
sum函数
sumfunction <- function(x) {
wow <- read.table(x, header=T)
#Sum of top 10
ten <- sum(wow$freq[1:10])
#Sum of 11-100
to100 <- sum(wow$freq[11:100])
#Sum of 101-1000
to1000 <- sum(wow$freq[101:1000])
#sum of 1001+
rest <- sum(wow$freq[-c(1:1000)])
blah <- c(ten,to100,to1000,rest)
}

library(data.table)
library(tools)
dir = "C:/temp/"
filenames <- list.files(path = dir, pattern = "*.txt", full.names = FALSE)
alltogether <- lapply(filenames, function(x) sumfunction(x))
data <- as.data.frame(data.table::transpose(alltogether), 
                  col.names =c("Top 10 ", "From 11 to 100", "From 101 to 1000", "From 1000 on "),
                  row.names = file_path_sans_ext(basename(filenames)))