计算每个站点的因子的新水平 (R)

Counting new levels of a factor per site (R)

我需要生成一个 table 来计算每个站点的新水平因素。 我的代码是这样的

# Data creation
f = c("red", "green", "blue", "orange", "yellow")
f = factor(f)
d = data.frame(
  site = 1:10,
  color1= c(
    "red", "red", "green", "green",  "green", 
    "blue","green", "blue", "orange", "yellow"
  ),
  color2= c(
    "green", "green",  "green", "blue","green", 
    "blue", "orange", "yellow","red", "red"
  )
)
d$color1 = factor( d$color1 , levels = levels(f) )
d$color2 = factor( d$color2 , levels = levels(f) )
d

它向我展示了这个 table

我需要计算每个新站点中有多少种新颜色。只计算第一次出现,不重复。结果 table 像这样。

此图中计算了每个站点的不重复颜色。

是否有 dplyr 方法来找到这个输出?

你可以这样做:

library(tidyverse)
d %>%
  pivot_longer(cols = -site) %>%
  mutate(newColors = duplicated(value)) %>%
  group_by(site) %>%
  mutate(newColors = sum(!newColors)) %>%
  ungroup() %>%
  pivot_wider()

给出:

# A tibble: 10 x 4
    site newColors color1 color2
   <int>     <int> <fct>  <fct> 
 1     1         2 red    green 
 2     2         0 red    green 
 3     3         0 green  green 
 4     4         1 green  blue  
 5     5         0 green  green 
 6     6         0 blue   blue  
 7     7         1 green  orange
 8     8         1 blue   yellow
 9     9         0 orange red   
10    10         0 yellow red   

请注意,第 9 行的情况有所不同,您有一个 1,但是两种颜色(橙色和红色)都已经出现在前面的行中。