从数据框粘贴一行以匹配另一个数据框的行长 [英] Paste a row from a dataframe to match the length of rows of another dataframe

查看:77
本文介绍了从数据框粘贴一行以匹配另一个数据框的行长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月以来,我一直在RStudio工作,一件事很难办.我有一个包含多个csv文件的目录,需要在RStudio中导入.每个名称和日期都有多个文件.

I'm working with RStudio since a few months now and I'm having a hard time with one thing. I have a directory with multiple csv files that I need to import in RStudio. There are multiples files per name and date.

csv文件的格式化方式实际上很奇怪. CSV的所有数据(数字)都从第7行开始.问题在于,需要分别提取所收集文件中的信息(名称,日期,设备等).

The way the csv file is formatted is actually pretty weird. All data (numbers) in the csv's s start at the row #7. The problem is that the informations from the collected file (name, date, equipment, etc) need to be extracted separately.

基本上,for循环中的temp数据帧都具有不同的行数(+200).另一方面,info数据帧都只有一行(每个csv一行).

Basically, the temp dataframes in the for loop all have a different numbers of rows (+200). On the other hand, the info dataframes all have a single row (one row per csv).

我想将两个文件与在关联数据df_groinbar的长度(在csv中)重复的info行绑定在一起.不要忘记每个csv(df_groinbar数据帧)的长度不同,因此需要为每个csv调整infodf_groinbar的绑定.

I would like to bind the two files together with the info row duplicated for the length of the associated data df_groinbar (in the csv). Don't forget that the length of each csv (df_groinbar dataframe) is different, so the bind of info and df_groinbar would need to be ajusted for each csv.

df_groinbar <- data.frame()
info <- data.frame()
for (i in list.files("/Users/Nicolas/Dropbox/Groin Bar/"))
{
  type <- str_extract(i, "([A-Z]+)")
  temp <- read_csv(i, skip = 6, col_names = c("elapsed_time", "left_squeeze", "right_squeeze", "left_pull", "right_pull"))
  info_temp <- select(read_csv(i, skip = 2, n_max = 1), 1:6)
  df_groinbar <- rbind(df_groinbar, temp)
  info <- rbind(info, info_temp)
}

我已经尝试过smartbind函数,并且还有很多,但是没有用.

I've tried smartbind functions and many more and nothing worked.

非常感谢!

尼古拉斯

推荐答案

这是您要的吗?

library(tidyverse)

filePattern <- "\\.csv$"
fileList <- list.files(path = "./Csv test/", recursive = FALSE,
                       pattern = filePattern, full.names = TRUE)

read_file_custom <- function(fileName) {

  # skip 6 lines and select only the first 5 columns
  dat <- readr::read_csv(file = fileName, skip = 6, col_names = FALSE) %>% 
    select(., 1:5) 

  colName <- c("TimeFrame", "Left(squeeze)", "Left(pull)", "Right(squeeze)", "Right(pull)")
  names(dat) <- colName

  # now read the 3rd and 4th lines & keep only the first 6 columns
  indi_info <- readr::read_csv(file = fileName, skip = 2, col_names = TRUE, n_max = 1) %>% 
    select(., 1:6)

  # transfer individual data to dat
  dat <- dat %>% 
    mutate(NAME   = indi_info$NAME,
           DATE   = indi_info$DATE,
           TIME   = indi_info$TIME,
           DEVICE = indi_info$DEVICE,
           MODE   = indi_info$MODE,
           TEST   = indi_info$TEST)

  return(dat)
}

# Loop through all the files using map_df, read data 
# and create a FileName column to store filenames
# Clean up filename: remove file path and extension
# Bind all files together

result <- fileList %>%
  purrr::set_names(nm = (basename(.) %>% tools::file_path_sans_ext())) %>%
  purrr::map_df(read_file_custom, .id = "FileName") 
result

#> # A tibble: 10,460 x 12
#>    FileName        TimeFrame `Left(squeeze)` `Left(pull)` `Right(squeeze)`
#>    <chr>               <dbl>           <dbl>        <dbl>            <dbl>
#>  1 Arianne-Robill~    0.0200          -1.00          2.25           -1.25 
#>  2 Arianne-Robill~    0.0400          -1.00          2.25           -1.25 
#>  3 Arianne-Robill~    0.0600          -1.00          2.25           -1.25 
#>  4 Arianne-Robill~    0.0800          -1.00          2.25           -1.25 
#>  5 Arianne-Robill~    0.100           -1.00          2.25           -1.25 
#>  6 Arianne-Robill~    0.120           -1.00          2.00           -1.25 
#>  7 Arianne-Robill~    0.140           -1.00          2.00           -1.00 
#>  8 Arianne-Robill~    0.160           -0.750         2.00           -1.00 
#>  9 Arianne-Robill~    0.180           -0.750         1.75           -0.750
#> 10 Arianne-Robill~    0.200           -0.750         1.75           -0.750
#> # ... with 10,450 more rows, and 7 more variables: `Right(pull)` <dbl>,
#> #   NAME <chr>, DATE <chr>, TIME <time>, DEVICE <chr>, MODE <chr>,
#> #   TEST <chr>

reprex程序包(v0.2.0)创建于2018-03-28.

Created on 2018-03-28 by the reprex package (v0.2.0).

这篇关于从数据框粘贴一行以匹配另一个数据框的行长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆