R语言 计算两列中数字的出现次数

vq8itlhq  于 9个月前  发布在  其他
关注(0)|答案(2)|浏览(111)

我有一个如下的嵌套框架。我想添加列count 1和count 2,这样所有的值都在col 1和col 2中计数。因此,第1行的count 1将是4,因为10在两列中出现了4次,Freqcol 1将是4/6

ID col1 col2
 1   10   12
 2   10   15
 3   10   10

字符串
所需输出:

ID col1 col2 count1 count2 Freqcol1 Freqcol2
 1   10   12      4      1     0.66     0.16
 2   10   15      4      1     0.66     0.16
 3   10   10      4      4     0.66     0.66


这是我尝试的,似乎给予错误的输出。

count_occurrences <- function(data) {
  unique_numbers <- unique(c(data$col1, data$col2)) 
  # Count occurrences for each number in Column1 
  count1 <- table(data$col1) 
  data$occurrences_col1 <- count_col1[match(data$col1, unique_numbers)] 
  # Count occurrences for each number in Column2 
  count_col2 <- table(data$col2) 
  data$occurrences_col2 <- count_col2[match(data$col2, unique_numbers)] 
  return(data) } 
# Count occurrences and add to the data frame 
result_data <- count_occurrences(mydata) 
print(result_data)

xytpbqjk

xytpbqjk1#

你可以用R来实现:

a <- unlist(df[-1])
b <- array(table(a)[as.character(a)], dim(df[-1]))
cbind(df, count = b, FreqCol = b/length(b))

  ID col1 col2 count.1 count.2 FreqCol.1 FreqCol.2
1  1   10   12       4       1 0.6666667 0.1666667
2  2   10   15       4       1 0.6666667 0.1666667
3  3   10   10       4       4 0.6666667 0.6666667

字符串

jv2fixgn

jv2fixgn2#

你可以试试pivot_*

df %>%
  pivot_longer(-ID) %>%
  mutate(count = n(), .by = value) %>%
  mutate(freq = count / n()) %>%
  pivot_wider(values_from = c(value, count, freq))

字符串
这给

# A tibble: 3 × 7
     ID value_col1 value_col2 count_col1 count_col2 freq_col1 freq_col2
  <int>      <int>      <int>      <int>      <int>     <dbl>     <dbl>
1     1         10         12          4          1     0.667     0.167
2     2         10         15          4          1     0.667     0.167
3     3         10         10          4          4     0.667     0.667

相关问题