0

I need help to modify my code to do the following tasks... I've used help from the following questions and answers thus far

Here are things i hope to be able to do with my code...

  1. I need to read in several files from a folder
  2. I will like to use the name of each of the files in the folder to add a column. I was able to do this simply with 'mutate' but for a single file
  3. I will like to save the result of each file separately and also combine to a single file
  4. I also want to keep the code for reading the files separate from the function, so i can apply to other projects.
  5. I'm trying to avoid using the 'loop' statements

Here is the sample of my incomplete code which gives error

library(tidyverse)
library(readr)
cleaningdata<- function(data){
  data$Label<-gsub(".tif", "", data$Label)
  data %>% select(Label:Solidity) %>%group_by(Label)%>%
    mutate(view = seq_along(Label), Station="T1-1")%>%
    rename(Species = Label)%>%
    mutate(view = recode(view, "1" = "a","2" = "b","3" = "c"))
}

filenames <- list.files("Data", pattern="*.txt", full.names=TRUE)
ldf <- lapply(filenames, read.txt)
res <- lapply(ldf, cleaningdata)

Here is a sample of my dataset Data Folder and below is my work thus far

Compo
  • 36,585
  • 5
  • 27
  • 39
Hammao
  • 801
  • 1
  • 9
  • 28
  • `read.txt` is not a valid `tidyverse`, `readr` or `base` function. Did you mean to use `readr::read_tsv`? The file format dictates which function you should use to read your data, not the file extension. If so, your code currently does not throw an error so you will need to elaborate – ruaridhw Feb 11 '18 at 15:22
  • @ruaridhw, Please look at the things i want to do listed, they part you commented on is just to read in the file and doesn't really change the problem. – Hammao Feb 11 '18 at 15:27
  • From what you've provided, that *is* the problem. You've specified that the sample throws an error and I've indicated why and how to fix it. I would suggest including your error in the question or fleshing out the sample a bit more as each of these points have been answered already on SO. – ruaridhw Feb 11 '18 at 15:34
  • @ruaridhw, this is the error "Warning message: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default"... My problem is that i still don't fully understand how the lapply work. And as i said, i just need help to modify my code to be able to do the four listed tasks because if my code was working, i won't be asking for help. – Hammao Feb 11 '18 at 19:24
  • @ruaridhw...By the way, i replace 'read.txt' with 'read.delim2' still not working – Hammao Feb 11 '18 at 19:31

1 Answers1

1

The fs package contains the useful dir_map function, which applies a function to each file in the path. If you need more control over the files to use, you could alternatively pipe a vector of the filenames into purrr::map() instead.

Your error Warning message: Unreplaced values treated as NA as .x is not compatible. Please specify replacements exhaustively or supply .default was because you were recoding 1, 2, 3 to a, b, c but one of the Species had 6 rows so 4, 5, 6 were recoded to NA. I've used letters[n] to avoid this problem.

library(tidyverse)
library(fs)

result <- dir_map(path = 'Data', fun = function(filepath) {
  read_tsv(filepath) %>%
    select(-1) %>%
    rename(Species = Label) %>%
    mutate(Species = sub('.tif$', '', Species)) %>%
    group_by(Species) %>%
    mutate(
      View = seq_along(Species),
      View = letters[View], # a, b, c, etc. instead of 1, 2, 3, etc.
      Station = sub('.txt$', '', basename(filepath))
    )
})

# get rows from second file
result[[2]]

# bind rows from all files
result %>% bind_rows()
Greg
  • 487
  • 5
  • 15