pivot_wider 仅在 R 中的一列上
pivot_wider only on one column in R
这是我的 df:
Analyte name Limit_Type
<chr> <chr> <chr>
1 " BOD(C)" Limit1 " 50%ile"
2 " BOD(C)" Limit1_Value "10"
3 " CBOD(C)" Limit1 " 90%ile"
4 " CBOD(C)" Limit1_Value "15"
5 " CBOD(C)" Limit2 " NA"
6 " CBOD(C)" Limit2_Value NA
我想基本上“传播”或 pivot_wider()
'Limit_Type' 列,以便我的 df 看起来像这样:
Analyte Limit_Type Limit
<chr> <chr> <chr>
1 " BOD(C)" " 50%ile" 10
2 " CBOD(C)" " 90%ile" 15
3 " CBOD(C)" "NA" NA
这可以用 dplyr 实现吗?
谢谢。
您可以根据 'Value'
的存在将 name
列更改为 Limit
和 Limit_Type
。使用 pivot_wider
以宽格式获取数据
library(dplyr)
library(tidyr)
df %>%
mutate(name = ifelse(grepl('Value', name), 'Limit', 'Limit_Type')) %>%
pivot_wider(names_from = name, values_from = Limit_Type, values_fn = list) %>%
unnest(cols = c(Limit_Type, Limit))
# Analyte Limit_Type Limit
# <chr> <chr> <chr>
#1 " BOD(C)" " 50%ile" 10
#2 " CBOD(C)" " 90%ile" 15
#3 " CBOD(C)" " NA" NA
数据
df <- structure(list(Analyte = c(" BOD(C)", " BOD(C)", " CBOD(C)",
" CBOD(C)", " CBOD(C)", " CBOD(C)"), name = c("Limit1", "Limit1_Value",
"Limit1", "Limit1_Value", "Limit2", "Limit2_Value"), Limit_Type = c(" 50%ile",
"10", " 90%ile", "15", " NA", NA)), class = "data.frame", row.names = c(NA, -6L))
这是我的 df:
Analyte name Limit_Type
<chr> <chr> <chr>
1 " BOD(C)" Limit1 " 50%ile"
2 " BOD(C)" Limit1_Value "10"
3 " CBOD(C)" Limit1 " 90%ile"
4 " CBOD(C)" Limit1_Value "15"
5 " CBOD(C)" Limit2 " NA"
6 " CBOD(C)" Limit2_Value NA
我想基本上“传播”或 pivot_wider()
'Limit_Type' 列,以便我的 df 看起来像这样:
Analyte Limit_Type Limit
<chr> <chr> <chr>
1 " BOD(C)" " 50%ile" 10
2 " CBOD(C)" " 90%ile" 15
3 " CBOD(C)" "NA" NA
这可以用 dplyr 实现吗?
谢谢。
您可以根据 'Value'
的存在将 name
列更改为 Limit
和 Limit_Type
。使用 pivot_wider
library(dplyr)
library(tidyr)
df %>%
mutate(name = ifelse(grepl('Value', name), 'Limit', 'Limit_Type')) %>%
pivot_wider(names_from = name, values_from = Limit_Type, values_fn = list) %>%
unnest(cols = c(Limit_Type, Limit))
# Analyte Limit_Type Limit
# <chr> <chr> <chr>
#1 " BOD(C)" " 50%ile" 10
#2 " CBOD(C)" " 90%ile" 15
#3 " CBOD(C)" " NA" NA
数据
df <- structure(list(Analyte = c(" BOD(C)", " BOD(C)", " CBOD(C)",
" CBOD(C)", " CBOD(C)", " CBOD(C)"), name = c("Limit1", "Limit1_Value",
"Limit1", "Limit1_Value", "Limit2", "Limit2_Value"), Limit_Type = c(" 50%ile",
"10", " 90%ile", "15", " NA", NA)), class = "data.frame", row.names = c(NA, -6L))