对几个文本文件进行计算并从中创建数据帧R

问题描述 投票:0回答:2

我正在尝试从我正在进行的计算中创建一个表到几个文本文件。我认为这可能需要某种循环,但我仍然坚持如何继续。我尝试了不同的循环但似乎没有工作。我已经设法用一个文件做我想要的。这是我的工作代码:

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)

数据框最终看起来像这样:

>View(df)
         Sum
10       0.1745092
11-100   0.2926735
101-1000 0.4211533
1000+    0.1116640

这非常适合制作堆叠的条形图,我这样做了。但是,这仅适用于一个文本文件。我有几个相同的文件。它们都具有相同的列名,因此我知道所有这些都将使用DF $ freq列进行计算。在对每个文件进行计算后如何制作表格?我想保留文本文件的名称作为样本名称,这样当我制作一个联合堆叠条形图时,所有名称都将存在。此外,在编写新表/数据帧时,定位数据的最佳方法是什么?

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

r dataframe
2个回答
1
投票

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

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

如果你的dfs长度不等,你可能会对ID列产生一些问题,所以这只是部分答案。使用缩短的数据集示例可以做得更好。任何人都可以权衡创建id列吗?可能已经对它进行了几次编辑...


0
投票

我想我解决了!它给了我想要的数据帧,从中我可以使堆叠的条形图显示数据。

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)))

这给了我想要的数据帧。我没有将“前10,11-100,101-1000,1000 +”作为行名称,而是将它们更改为列名,而是将每个文本文件的名称变为行名。 file_path_sans_ext(basename(filenames))确保只保留文件名并删除扩展名。

我希望这可以帮助任何阅读此内容的人!再次感谢你!我喜欢这个平台,因为只是成为这个环境的一部分让我思考并始终努力让自己更好地在R。

如果有人有任何意见,这将是伟大的! <3

© www.soinside.com 2019 - 2024. All rights reserved.