In previous projects for this course, the file we were to use has been pretty simple to read in. Using read.csv/table(file = , sep = “,”, header = TRUE) has become almost muscle memory. This file contains a patient survey from the American Gut Project for the sequencing of human gut and oral microbiome. This is a txt file with no numerical or categorical data, but a list of survey questions and answers. The following problem will be to read in the txt file and make it human readable
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
read.delim is useful for txt files, and using sep = ’ signifies that each group or line of text is separated by a tab rather than a comma or a line break.
biosample <- read.delim(file = "biosample_result (1).txt", sep = '\t', header = FALSE)
head(biosample, 30)
## V1
## 1 1: american gut project; 10317.X00185902
## 2 Identifiers: BioSample: SAMEA112990854; SRA: ERS14985877
## 3 Organism: human gut metagenome
## 4 Attributes:
## 5 /ENA-CHECKLIST
## 6 /ENA-FIRST-PUBLIC
## 7 /ENA-LAST-UPDATE
## 8 /External Id
## 9 /INSDC center alias
## 10 /INSDC center name
## 11 /INSDC first public
## 12 /INSDC last update
## 13 /INSDC status
## 14 /Submitter Id
## 15 /acid_reflux
## 16 /acne_medication
## 17 /acne_medication_otc
## 18 /add_adhd
## 19 /age_cat
## 20 /alcohol_consumption
## 21 /alcohol_frequency
## 22 /alcohol_types_beercider
## 23 /alcohol_types_red_wine
## 24 /alcohol_types_sour_beers
## 25 /alcohol_types_spiritshard_alcohol
## 26 /alcohol_types_unspecified
## 27 /alcohol_types_white_wine
## 28 /allergic_to_i_have_no_food_allergies_that_i_know_of
## 29 /allergic_to_peanuts
## 30 /allergic_to_shellfish
## V2
## 1
## 2
## 3
## 4
## 5 ERC000011
## 6 4/28/2023
## 7 4/28/2023
## 8 SAMEA112990854
## 9 UCSDMI
## 10 University of California San Diego Microbiome Initiative
## 11 2023-04-28T16:20:26Z
## 12 2023-04-28T16:20:26Z
## 13 public
## 14 qiita_sid_10317:10317.X00185902
## 15 i do not have this condition
## 16 no
## 17 no
## 18 i do not have this condition
## 19 30s
## 20 yes
## 21 occasionally (1-2 times/week)
## 22 TRUE
## 23 TRUE
## 24 TRUE
## 25 TRUE
## 26 FALSE
## 27 FALSE
## 28 FALSE
## 29 FALSE
## 30 FALSE
This has resulted in a dataframe of two columns. The first column consists of basic information in regards to the study, then followed by the list of survey questions. The other column contains empty rows until the answers to the survey questions appear.
Currently, the columns are named V1 and V2, and I would like to keep it that way for now until I finalize the layout.
Each of the rows in the first column begin with a forward slash that I would like to remove and each space is represented with an underscore that I would to replace with a space.
biosample <- biosample %>%
mutate(V1 = str_remove_all(V1, "/")) %>%
mutate(V1 = str_replace_all(V1, "_"," "))
head(biosample, 30)
## V1
## 1 1: american gut project; 10317.X00185902
## 2 Identifiers: BioSample: SAMEA112990854; SRA: ERS14985877
## 3 Organism: human gut metagenome
## 4 Attributes:
## 5 ENA-CHECKLIST
## 6 ENA-FIRST-PUBLIC
## 7 ENA-LAST-UPDATE
## 8 External Id
## 9 INSDC center alias
## 10 INSDC center name
## 11 INSDC first public
## 12 INSDC last update
## 13 INSDC status
## 14 Submitter Id
## 15 acid reflux
## 16 acne medication
## 17 acne medication otc
## 18 add adhd
## 19 age cat
## 20 alcohol consumption
## 21 alcohol frequency
## 22 alcohol types beercider
## 23 alcohol types red wine
## 24 alcohol types sour beers
## 25 alcohol types spiritshard alcohol
## 26 alcohol types unspecified
## 27 alcohol types white wine
## 28 allergic to i have no food allergies that i know of
## 29 allergic to peanuts
## 30 allergic to shellfish
## V2
## 1
## 2
## 3
## 4
## 5 ERC000011
## 6 4/28/2023
## 7 4/28/2023
## 8 SAMEA112990854
## 9 UCSDMI
## 10 University of California San Diego Microbiome Initiative
## 11 2023-04-28T16:20:26Z
## 12 2023-04-28T16:20:26Z
## 13 public
## 14 qiita_sid_10317:10317.X00185902
## 15 i do not have this condition
## 16 no
## 17 no
## 18 i do not have this condition
## 19 30s
## 20 yes
## 21 occasionally (1-2 times/week)
## 22 TRUE
## 23 TRUE
## 24 TRUE
## 25 TRUE
## 26 FALSE
## 27 FALSE
## 28 FALSE
## 29 FALSE
## 30 FALSE
To make the observations look better, all text can be capitalized to maintain acronyms and symbols.
biosample <- biosample %>%
mutate(across(c(V1, V2), toupper))
The columns can now be renamed to headers that are recgonizable and describe the oberservations within each column.
biosample <- biosample %>%
rename(
"Attribute" = "V1",
"Response" = "V2"
)
head(biosample, 30)
## Attribute
## 1 1: AMERICAN GUT PROJECT; 10317.X00185902
## 2 IDENTIFIERS: BIOSAMPLE: SAMEA112990854; SRA: ERS14985877
## 3 ORGANISM: HUMAN GUT METAGENOME
## 4 ATTRIBUTES:
## 5 ENA-CHECKLIST
## 6 ENA-FIRST-PUBLIC
## 7 ENA-LAST-UPDATE
## 8 EXTERNAL ID
## 9 INSDC CENTER ALIAS
## 10 INSDC CENTER NAME
## 11 INSDC FIRST PUBLIC
## 12 INSDC LAST UPDATE
## 13 INSDC STATUS
## 14 SUBMITTER ID
## 15 ACID REFLUX
## 16 ACNE MEDICATION
## 17 ACNE MEDICATION OTC
## 18 ADD ADHD
## 19 AGE CAT
## 20 ALCOHOL CONSUMPTION
## 21 ALCOHOL FREQUENCY
## 22 ALCOHOL TYPES BEERCIDER
## 23 ALCOHOL TYPES RED WINE
## 24 ALCOHOL TYPES SOUR BEERS
## 25 ALCOHOL TYPES SPIRITSHARD ALCOHOL
## 26 ALCOHOL TYPES UNSPECIFIED
## 27 ALCOHOL TYPES WHITE WINE
## 28 ALLERGIC TO I HAVE NO FOOD ALLERGIES THAT I KNOW OF
## 29 ALLERGIC TO PEANUTS
## 30 ALLERGIC TO SHELLFISH
## Response
## 1
## 2
## 3
## 4
## 5 ERC000011
## 6 4/28/2023
## 7 4/28/2023
## 8 SAMEA112990854
## 9 UCSDMI
## 10 UNIVERSITY OF CALIFORNIA SAN DIEGO MICROBIOME INITIATIVE
## 11 2023-04-28T16:20:26Z
## 12 2023-04-28T16:20:26Z
## 13 PUBLIC
## 14 QIITA_SID_10317:10317.X00185902
## 15 I DO NOT HAVE THIS CONDITION
## 16 NO
## 17 NO
## 18 I DO NOT HAVE THIS CONDITION
## 19 30S
## 20 YES
## 21 OCCASIONALLY (1-2 TIMES/WEEK)
## 22 TRUE
## 23 TRUE
## 24 TRUE
## 25 TRUE
## 26 FALSE
## 27 FALSE
## 28 FALSE
## 29 FALSE
## 30 FALSE
The dataframe can now be exported into one of two human readable formats, txt or csv. txt file is usually used for raw text data, which this is a file filled with that and should be easy to read in since all the data is tab separated. csv can be used if you wish to maintain the dataframe structure of two columns with multiple response rows.
write.table(biosample, file = "gutbiome.txt", sep = "\t", row.names = FALSE)
write.csv(biosample, file = "gutbiome.csv", sep = "\t", row.names = FALSE)
## Warning in write.csv(biosample, file = "gutbiome.csv", sep = "\t", row.names =
## FALSE): attempt to set 'sep' ignored