假設我有這個資料集:
col1 col2
1 2 1
2 1 1
3 1 2
4 1 2
5 1 2
6 1 1
7 2 1
8 2 2
我將如何創建一個列來計算“1”或“2”出現在列中的次數,使其看起來像這樣:
col1 col2 count_1 count_2
1 2 1 1 1
2 1 1 2 0
3 1 2 1 1
4 1 2 1 1
5 1 2 1 1
6 1 1 2 0
7 2 1 1 1
8 2 2 0 2
uj5u.com熱心網友回復:
我們可以rowSums
在創建的邏輯矩陣上使用要通過回圈遍歷這些值來進行比較的值
df1[paste0("count_", seq_along(df1))] <- lapply(1:2,
function(x) rowSums(df1 == x))
-輸出
> df1
col1 col2 count_1 count_2
1 2 1 1 1
2 1 1 2 0
3 1 2 1 1
4 1 2 1 1
5 1 2 1 1
6 1 1 2 0
7 2 1 1 1
8 2 2 0 2
資料
df1 <- structure(list(col1 = c(2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), col2 = c(1L,
1L, 2L, 2L, 2L, 1L, 1L, 2L)), class = "data.frame",
row.names = c("1",
"2", "3", "4", "5", "6", "7", "8"))
uj5u.com熱心網友回復:
風格tidyverse
:
library(dplyr)
library(purrr)
df1 %>%
mutate(map_dfc(1:2, ~ transmute(df1, "count_{.x}" := rowSums(across(everything()) == .x))))
# col1 col2 count_1 count_2
# 1 2 1 1 1
# 2 1 1 2 0
# 3 1 2 1 1
# 4 1 2 1 1
# 5 1 2 1 1
# 6 1 1 2 0
# 7 2 1 1 1
# 8 2 2 0 2
uj5u.com熱心網友回復:
如果您正在計算指定列中的數字 1 到 n:
n = 2L
inp_col = sprintf("col%d", 1L:2L)
df[sprintf("count_%d", 1L:n)] = t(apply(df[inp_col], 1L, tabulate, nbins = n))
# col1 col2 count_1 count_2
# 1 2 1 1 1
# 2 1 1 2 0
# 3 1 2 1 1
# 4 1 2 1 1
# 5 1 2 1 1
# 6 1 1 2 0
# 7 2 1 1 1
# 8 2 2 0 2
資料:
df = data.frame(
col1 = c(2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L),
col2 = c(1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L)
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/527005.html
標籤:r
下一篇:在資料框中找到某個值