如何使用 R 操作多个变量 (79x1532)?
How to manipulate multiple variables (79x1532) with R?
我是一个努力学习数据整理的新手。目前我陷入了 79x1532 数据帧的数据争论中。
我的数据框有 79 个样本行和 1,532 列 - 2 个样本标识符列,1,530 个化学浓度列 (79x1,532),如下所示。
wc_sample_conc_df
# A tibble: 79 x 1,532
`Sample Name` `Sample Number` `1,2,4-Trichlor… `1,2-Dibromo-3-… `o-Dichlorobenz… `1,3-Dichlorobe… `p-Dichlorobenz… `1,4-Naphthoqui…
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 CAS 001 A191916 <276 <423 <340 <340 <340 <6.32e+006
2 CAS 002 A191917 <276 <423 <340 <340 <340 <6.32e+006
3 CAS 003 A191918 <276 <423 <340 <340 <340 <6.32e+006
4 CAS 004 A191919 <276 <423 <340 <340 <340 <6.32e+006
5 CAS 005 A191920 <276 <423 <340 <340 <340 <6.32e+006
6 CAS 006 A191921 <276 <423 <340 <340 <340 <6.32e+006
7 CAS 007 A191922 <276 <423 <340 <340 <340 <6.32e+006
8 CAS 08 A191923 <276 <423 <340 <340 <340 <6.32e+006
9 CAS 009 A191924 <276 <423 <340 <340 <340 <6.32e+006
10 CAS 010 A191925 <276 <423 <340 <340 <340 <6.32e+006
# … with 69 more rows, and 1,524 more variables:
我的目标是
1 识别包含“<”的变量;
2 删除“<”并将变量类型从字符转换为数字;
3 将“<”原来存在的值除以 2
例如,我想在任务结束时将[1:10,3]中的值“<276”操作为138。
4 在步骤 1-3 之后,我的最终目标是为每个变量添加具有 0 或 1 的标志列。
例如[1:10,3]中的138和同一列[3]中较小的变量将标记为flag == 0。其他大于138的变量在同一列[3]中将标记为 flag == 1。我将在 flag 中使用此值来计算样本中化学物质的流行率 (n=79)。
然而,我仍然卡在第1-3步。
对于任务 1-3,我对一种化学品使用了以下命令,1,2,4-Trichlorobenzene Concentration
(wc_sample_conc_df[1,3]
)
wc_sample_conc_df %>%
mutate(`1,2,4-Trichlorobenzene Concentration` = case_when(
grepl("<", `1,2,4-Trichlorobenzene Concentration`) ~ `1,2,4-Trichlorobenzene Concentration`)) %>% str_remove("<") %>% as.numeric() %>% . / 2
TRUE ~ numeric(`1,2,4-Trichlorobenzene Concentration`)
我收到此错误消息,但不知道如何修复或从哪里重新开始..
Error in .(.) : could not find function "."
In addition: Warning messages:
1: In stri_replace_first_regex(string, pattern, fix_replacement(replacement), :
argument is not an atomic vector; coercing
2: In wc_sample_conc_df %>% mutate(`1,2,4-Trichlorobenzene Concentration` = case_when(grepl("<", : NAs introduced by coercion
我也想知道是否有更好的方法来使用数据框 (79x1,532) 中的整个变量执行此过程。我试图找到一些示例,但只能找到较小数据框的简单示例。预先感谢您的帮助!
=====
@akrun 谢谢你的回答。我尝试使用您的回答中建议的命令,但又遇到了另一个错误。你能帮我添加或删除哪个括号吗?
> wc_sample_conc_df %>%
+ mutate(across(where(~ any(str_detect(., fixed("<"))),
+ ~ {tmp <- as.numeric(str_remove(., fixed("<")))
+ case_when(str_detect(., fixed("<"))~ tmp/2, TRUE ~ tmp)))
Error: unexpected ')' in:
" ~ {tmp <- as.numeric(str_remove(., fixed("<")))
case_when(str_detect(., fixed("<"))~ tmp/2, TRUE ~ tmp))"
我们可以循环 across
列 where
有 any
个具有 <
的子字符串,然后使用 case_when
来划分那些具有 <
在每列中,在转换为数字后,否则 return 数字元素
library(dplyr)
library(stringr)
wc_sample_conc_df %>%
mutate(across(where(~ any(str_detect(., fixed("<")))),
~ {tmp <- as.numeric(str_remove(., fixed("<")))
case_when(str_detect(., fixed("<"))~ tmp/2, TRUE ~ tmp)}))
我是一个努力学习数据整理的新手。目前我陷入了 79x1532 数据帧的数据争论中。
我的数据框有 79 个样本行和 1,532 列 - 2 个样本标识符列,1,530 个化学浓度列 (79x1,532),如下所示。
wc_sample_conc_df
# A tibble: 79 x 1,532
`Sample Name` `Sample Number` `1,2,4-Trichlor… `1,2-Dibromo-3-… `o-Dichlorobenz… `1,3-Dichlorobe… `p-Dichlorobenz… `1,4-Naphthoqui…
<chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 CAS 001 A191916 <276 <423 <340 <340 <340 <6.32e+006
2 CAS 002 A191917 <276 <423 <340 <340 <340 <6.32e+006
3 CAS 003 A191918 <276 <423 <340 <340 <340 <6.32e+006
4 CAS 004 A191919 <276 <423 <340 <340 <340 <6.32e+006
5 CAS 005 A191920 <276 <423 <340 <340 <340 <6.32e+006
6 CAS 006 A191921 <276 <423 <340 <340 <340 <6.32e+006
7 CAS 007 A191922 <276 <423 <340 <340 <340 <6.32e+006
8 CAS 08 A191923 <276 <423 <340 <340 <340 <6.32e+006
9 CAS 009 A191924 <276 <423 <340 <340 <340 <6.32e+006
10 CAS 010 A191925 <276 <423 <340 <340 <340 <6.32e+006
# … with 69 more rows, and 1,524 more variables:
我的目标是
1 识别包含“<”的变量;
2 删除“<”并将变量类型从字符转换为数字;
3 将“<”原来存在的值除以 2
例如,我想在任务结束时将[1:10,3]中的值“<276”操作为138。
4 在步骤 1-3 之后,我的最终目标是为每个变量添加具有 0 或 1 的标志列。
例如[1:10,3]中的138和同一列[3]中较小的变量将标记为flag == 0。其他大于138的变量在同一列[3]中将标记为 flag == 1。我将在 flag 中使用此值来计算样本中化学物质的流行率 (n=79)。
然而,我仍然卡在第1-3步。
对于任务 1-3,我对一种化学品使用了以下命令,1,2,4-Trichlorobenzene Concentration
(wc_sample_conc_df[1,3]
)
wc_sample_conc_df %>%
mutate(`1,2,4-Trichlorobenzene Concentration` = case_when(
grepl("<", `1,2,4-Trichlorobenzene Concentration`) ~ `1,2,4-Trichlorobenzene Concentration`)) %>% str_remove("<") %>% as.numeric() %>% . / 2
TRUE ~ numeric(`1,2,4-Trichlorobenzene Concentration`)
我收到此错误消息,但不知道如何修复或从哪里重新开始..
Error in .(.) : could not find function "."
In addition: Warning messages:
1: In stri_replace_first_regex(string, pattern, fix_replacement(replacement), :
argument is not an atomic vector; coercing
2: In wc_sample_conc_df %>% mutate(`1,2,4-Trichlorobenzene Concentration` = case_when(grepl("<", : NAs introduced by coercion
我也想知道是否有更好的方法来使用数据框 (79x1,532) 中的整个变量执行此过程。我试图找到一些示例,但只能找到较小数据框的简单示例。预先感谢您的帮助!
=====
@akrun 谢谢你的回答。我尝试使用您的回答中建议的命令,但又遇到了另一个错误。你能帮我添加或删除哪个括号吗?
> wc_sample_conc_df %>%
+ mutate(across(where(~ any(str_detect(., fixed("<"))),
+ ~ {tmp <- as.numeric(str_remove(., fixed("<")))
+ case_when(str_detect(., fixed("<"))~ tmp/2, TRUE ~ tmp)))
Error: unexpected ')' in:
" ~ {tmp <- as.numeric(str_remove(., fixed("<")))
case_when(str_detect(., fixed("<"))~ tmp/2, TRUE ~ tmp))"
我们可以循环 across
列 where
有 any
个具有 <
的子字符串,然后使用 case_when
来划分那些具有 <
在每列中,在转换为数字后,否则 return 数字元素
library(dplyr)
library(stringr)
wc_sample_conc_df %>%
mutate(across(where(~ any(str_detect(., fixed("<")))),
~ {tmp <- as.numeric(str_remove(., fixed("<")))
case_when(str_detect(., fixed("<"))~ tmp/2, TRUE ~ tmp)}))