That's perfect!! What is the purrr::reduce doing??
Thank you very much guys!
Hi guys,
I have probably an easy task but my R knowledge is not good enough. I have a column with COG-annotation categories, with some raws having multiple categories:
A
A
B|Q
B|Q
B|Q
R|P|G|E
R|P|G|E
R|P|G|E
I would like to split them (thus removing the | separator which I managed using awk) and then concatenated all the (here) 4 columns in only one, so I can count the total frequency of each category. I said R just because I'm going to make a plot afterwards, but also awk or similar are very welcome. Thanks a lot, S
if(!require("tidyverse")) install.packages("tidyverse")
library(tidyverse)
x <- read_csv("data.csv")
x <- str_replace_all(x$COG_CATEGORY, pattern = "\\|", replacement = " ")
x <- str_split(x, " ")
x <- purrr::reduce(x, c)
table(x)
Oh sorry. I edited the previous post. That's one column (called COG_CATEGORY) of a CSV file with many more columns and thousands of raws, I copied just few to give an idea. And that's what I would like:
A
A
B
B
B
R
R
R
Q
Q
Q
P
P
P
G
G
G
E
E
E
Ti finally have:
Category Frequency
A 2
B 3
R 3
..
Log in to answer this question.
Can you post what the data.frame looks like currently?
Oh sorry. I edited the previous post. That's one column (called COG_CATEGORY) of a CSV file with many more columns and thousands of raws, I copied just few to give an idea. And that's what I would like:
Ti finally have:
Input:
output:
That's awesome! I should learn/use more often those three commands, thanks a lot!