簡體   English   中英

在單個 csv 文件中的多個類似 csv 文件和 output 上的相同功能?

[英]Same functions on multiple similar csv files and output in a single csv file?

R 的新手在這里。

我發現了關於我的問題的類似線程和教程,但是由於我無法找出解決問題的方法,並且隨着我的項目截止日期臨近,我想尋求幫助。

我在一個目錄中有多個類似的 csv 文件,名稱相似(2fg,20fg,...)。 它們都包含 8 列和 360 行的數據框。 行代表時間,每列代表一系列測量值。

我想要做的是找到每列的平均值和 sd,然后是這 8 個平均值的集體平均值和 sd,最后是 plot 每個 csv 的時間序列(每條線都有不同的顏色)和比較集體平均值的直方圖(和 sd)的所有 csv。

So far I am able of finding the mean, sd and their collectives and plot the time series for a single csv file, using plot.ts(2fg, plot.type=c("single"), col=rainbow(ncol(2fg)))

What i am searching for is a way to do all that on every csv in my directory, then plot the histogram that will compare the collective means and output the plots and the collective means and sds of each csv on a single xlsx or csv file.

我正在研究的方法是將我所有的 csvs 放在一個列表中,使用

list <- list.files(pattern = "csv") 

然后嘗試使用 lapply 但我無法產生任何結果。

我希望我足夠清楚,並提前感謝您的幫助!

編輯:按照@Len Greski 在評論中提出的建議,我將所有文件放在一個列表中,同時為我的數據提供 header

    lista <- list.files(pattern = ".csv", full.names=TRUE)
myfiles <- lapply(lista,function(x) {
  y <- read.csv(x,stringsAsFactors=FALSE, header = FALSE, sep = ',',
                col.names = c("well_1", "well_2", "well_3", "well_4", "well_5",
                "well_6", "well_7", "well_8"))
  y$filename <- x
  y 
})

然后在一個大數據框中

 data <- do.call(rbind,myfiles)

 
# A tibble: 6 x 9
    well_1 well_2 well_3 well_4 well_5 well_6 well_7 well_8 filename          
       <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl> <chr>             
    1  0.158  0.218  0.152  0.189  0.205  0.190  0.181  0.153 ./1_s1_control.csv
    2  0.158  0.218  0.152  0.189  0.205  0.190  0.181  0.153 ./1_s1_control.csv
    3  0.158  0.218  0.152  0.189  0.204  0.190  0.181  0.153 ./1_s1_control.csv
    4  0.158  0.218  0.152  0.189  0.204  0.190  0.181  0.153 ./1_s1_control.csv
    5  0.159  0.218  0.151  0.189  0.204  0.190  0.181  0.153 ./1_s1_control.csv
    6  0.159  0.218  0.151  0.189  0.204  0.190  0.181  0.153 ./1_s1_control.csv

然后我嘗試了以下方法來計算均值和標准差,但出現錯誤

    # summarise by file (filename)
    data2 <- data %>% 
      group_by(filename) %>%
      summarise(., across(c(well_1, well_2, well_3, well_4, well_5, well_6, well_7, well_8)),
                         list(mean = mean, sd = sd), .names = "{col}.{fn}")

Error: `across()` must only be used inside dplyr verbs.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
In names(cols)[missing_names] <- names[missing_names] :
  number of items to replace is not a multiple of replacement length

歡迎任何建議或更正::)

這是一個如何完成問題中所有任務的示例,除了繪制時間序列和直方圖。 由於問題沒有提供可重復的示例,我們將使用我保存在 Github 上的Pokémon Stats數據,最初基於 Alberto Barradas 的 Kaggle 數據集。

每個文件包含一代神奇寶貝, gen01.csvgen07.csv 所有文件都跟蹤代表神奇寶貝基本統計數據的 13 個變量,如下圖所示。

在此處輸入圖像描述

首先,我們將下載數據,解壓縮 zip 文件,獲取文件名列表,並使用lapply()和 read.csv() 讀取它們。

if(!file.exists("pokemonData.zip")){
     download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
                   "pokemonData.zip",
                   method="curl",mode="wb")
     unzip("pokemonData.zip")
}

thePokemonFiles <- list.files("./pokemonData",pattern="*.csv",
                              full.names=TRUE)

pokemonDataFiles <- lapply(thePokemonFiles,function(x) {
    y <- read.csv(x,stringsAsFactors=FALSE)
    # uncomment next line to add a column that stores the file name
    # y$fileName <- x
    y 
})

接下來,我們使用do.call()將七個數據幀組合成一個數據幀。

# merge to single data frame
data <- do.call(rbind,pokemonDataFiles)

由於每個文件都包含一個映射到文件的列Generation ,因此我們可以將此變量與dplyr::group_by()一起使用,以按文件計算均值和標准差。

library(dplyr)
# summarise by file (generation)
data %>% group_by(Generation) %>%
     summarise(.,across(c(Total,HP,Attack,Defense,SpecialAtk,SpecialDef,Speed),
                        list(mean = mean, sd = sd), .names = "{col}.{fn}")) 


# A tibble: 7 x 15
  Generation Total.mean Total.sd HP.mean HP.sd Attack.mean Attack.sd Defense.mean
       <int>      <dbl>    <dbl>   <dbl> <dbl>       <dbl>     <dbl>        <dbl>
1          1       426.     115.    65.6  28.1        76.5      30.8         70.7
2          2       418.     120.    71.2  30.6        72.0      32.7         73.4
3          3       436.     136.    66.5  24.1        81.6      36.6         74.1
4          4       459.     120.    73.1  25.1        82.9      32.8         78.1
5          5       435.     108.    71.8  22.4        82.1      30.4         72.3
6          6       436.     115.    68.3  20.9        75.8      29.2         76.7
7          7       461.     123.    71.3  26.9        87.1      33.9         79.3
# … with 7 more variables: Defense.sd <dbl>, SpecialAtk.mean <dbl>,
#   SpecialAtk.sd <dbl>, SpecialDef.mean <dbl>, SpecialDef.sd <dbl>,
#   Speed.mean <dbl>, Speed.sd <dbl>

我們可以通過消除 dplyr 管道中的group_by() function 來計算文件間的均值和標准差。

# summarise across generations
summarise(data,across(c(Total,HP,Attack,Defense,SpecialAtk,SpecialDef,Speed),
               list(mean = mean, sd = sd), .names = "{col}.{fn}")) 


  Total.mean Total.sd  HP.mean  HP.sd Attack.mean Attack.sd Defense.mean
1   437.6293 120.4411 69.43561 25.665    79.82643  32.70236     74.39194
  Defense.sd SpecialAtk.mean SpecialAtk.sd SpecialDef.mean SpecialDef.sd
1   31.32974        73.39866      33.11673        72.37066      27.96375
  Speed.mean Speed.sd
1   68.20605 29.28088
> 

為了解決問題的繪圖部分,我們需要發布問題的人提供更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM