Reputation: 13
I am totally new to R and I'm trying to run a function from a specific package : getId.
It works like this :
data<-getId(c('Pestivirus A','Bos taurus','Homo'),taxaNames)
However I have a lot of different data like 'Pestivirus A'. I have all of the inputs in a tsv files like this :
Pestivirus A
Bos taurus
...
Is there a clean easy way to put all these 7000 inputs ?
So far I converted my results in a .txt file like this :
'Pestivirus A','Bos taurus','Homo'...
And I have tried to import this file like this :
> species <- read.csv("C:/Users/vdoinel/Desktop/5_species.txt")
But I get an error " Error in file(file, "rt") : impossible d'ouvrir la connexion" and "No such file or directory". But anyway even if it has worked I don't know what to do after.
Upvotes: 0
Views: 337
Reputation: 1277
Here are some hints:
read.csv
returns a "data.frame", which is a two dimensional data structure with rows and cols. Let us call this data.frme "x". Make sure to read the file with header=FALSE
, because you do not have colnames.items <- x[1,]
for (item in items) {...}
The details should be looked up in your preferred R reference book/site.
Upvotes: 1
Reputation: 10162
While there is nothing wrong with @cdalitz answer, I personally prefer the tidyverse
, which allows you to do the same things in a (imho) nicer way.
In your case:
library(tidyverse)
# list all .txt files, the $ signals end of text
files <- list.files(pattern = ".txt$")
# map the files to the function read_tsv (reads in the data);
# assign the filenames as names to the list
data <- map(files, read_tsv) %>% set_names(files)
# data is now a list containing the data of the files;
# each slot holds data from one file
# combine the list to a single dataset/data.frame with a new variable called "file" containing the file name
data_all <- bind_rows(data, .id = "file")
Upvotes: 0