英文:
How To add number if given in bracket and put calculated value under given catogeries
问题
以下是已经翻译好的部分:
我有一个文本文件。这个文本文件的数据类似于下面给出的示例。我想在R中处理这些数据,使括号中给出的所有值相加,并将值保留在一个类别下,就像示例中给出的那样。请帮助我,如何导入和处理文本文件,以获得我所期望的结果,我是编程新手。
文本文件看起来像下面这样
碳水化合物代谢
00010 糖解/糖异生 (27)
00020 柠檬酸循环 (TCA循环) (22)
00030 五碳糖磷酸途径 (19)
能量代谢
00190 氧化磷酸化 (68)
00710 光合生物中的碳固定 (16)
00720 原核生物中的碳固定途径 (10)
我需要输出为数据框,应该在添加括号中给出的值后看起来如下
V1 V2
碳水化合物代谢 68
能量代谢 94
英文:
I have a text file. This text file has data similar to the example given here down. I would like process the data in R in such a way that all value given in bracket should be added and keep the value under one catogeries as given in example. Kindly help, how to import and process the text file, to get my desire results, I am new to programming.
text file look like given below
Carbohydrate metabolism
00010 Glycolysis / Gluconeogenesis (27)
00020 Citrate cycle (TCA cycle) (22)
00030 Pentose phosphate pathway (19)
Energy metabolism
00190 Oxidative phosphorylation (68)
00710 Carbon fixation in photosynthetic organisms (16)
00720 Carbon fixation pathways in prokaryotes (10)
I nedd output in dtatfram, which should look like after adding values given in bracket under catogeris
V1 V2
Carbohydrate metabolism 68
Energy metabolism 94
答案1
得分: 0
这很复杂,因为实际上有两个数据框堆叠在一起。实现目标的一种方法是:1)创建一个分组变量,如果代谢是能量或碳水化合物,2)将字符串分割成能量的名称和值(值位于括号内,所以我们还需要去掉括号),3)使用 summarize()
按组总结所有内容。
library(tidyverse)
tt <- read_delim("
Carbohydrate metabolism
00010 Glycolysis / Gluconeogenesis (27)
00020 Citrate cycle (TCA cycle) (22)
00030 Pentose phosphate pathway (19)
Energy metabolism
00190 Oxidative phosphorylation (68)
00710 Carbon fixation in photosynthetic organisms (16)
00720 Carbon fixation pathways in prokaryotes (10)",
col_names = c("id", "name"))
tt <- tt %>%
# 创建一个分组变量以“分割”您的两个数据框
mutate(meta = as.character(replace(id, str_detect(id, "^[:digit:]+$"), NA))) %>%
fill(meta, .direction = "down") %>%
# 去掉中间数据框中的“列名”
filter(name != "metabolism") %>%
# 按括号拆分代谢的名称和值
extract("name", c("char", "value"), "(\\D*)(\\d.*)" ) %>%
# 去掉括号,减去“值”列中的最后一个字符
mutate(value = as.numeric(substring(value, 1, nchar(value)-1))) %>%
# 按分组变量求和
group_by(meta) %>%
summarise(sumvalue = sum(value))
print(tt)
# A tibble: 2 × 2
meta sumvalue
<chr> <dbl>
1 Carbohydrate 68
2 Energy 94
希望对你有所帮助。
英文:
This is tricky because you essentially have two dataframes stacked together. One way to achieve your goal is to 1) create a grouping variable if metabolism is energy or carbohydrate, 2) split up the string into the name of energy and the value (which is stuck inside parentheses, so we also need to get rid of those parentheses), and 3) use summarize()
to sum everything up by group.
library(tidyverse)
tt <- read_delim("
Carbohydrate metabolism
00010 Glycolysis / Gluconeogenesis (27)
00020 Citrate cycle (TCA cycle) (22)
00030 Pentose phosphate pathway (19)
Energy metabolism
00190 Oxidative phosphorylation (68)
00710 Carbon fixation in photosynthetic organisms (16)
00720 Carbon fixation pathways in prokaryotes (10)",
col_names = c("id", "name"))
tt <- tt %>%
# create a grouping variable to "divide" your two dataframes
mutate(meta = as.character(replace(id, str_detect(id, "^[:digit:]+$"), NA))) %>%
fill(meta, .direction = "down") %>%
# get rid of "column name" stuck in middle of dataframe
filter(name != "metabolism") %>%
# split up name of the metabolism and the value by the parenthesis
extract("name", c("char", "value"), "(\\D*)(\\d.*)") %>%
# get rid of parenthesis by subtracting last character in the column "value"
mutate(value = as.numeric(substring(value, 1, nchar(value)-1))) %>%
# sum up by grouping variable
group_by(meta) %>%
summarise(sumvalue = sum(value))
print(tt)
# A tibble: 2 × 2
meta sumvalue
<chr> <dbl>
1 Carbohydrate 68
2 Energy 94
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论