简体   繁体   English

R - 计数出现并乘以等级

[英]R - counting occurences and multiply by rank

I am unable to replicate a way of applying lists to occurences (or counts). 我无法复制将列表应用于出现(或计数)的方法。

I've been toying around with rowSums() but I can't figure out how to use it in a general way over multiple columns, which also should be multiplied by rank (see rank.list below) 我一直在rowSums()但我无法弄清楚如何在多列上以一般方式使用它,也应该乘以等级 (参见下面的rank.list

My data is shown below, what I want to do is: 我的数据如下所示,我想要做的是:

  • 1) Count the number of occurences per column (a form of communication) 1)计算每列的出现次数(一种通信形式)
  • 2) Multiply that number by a given rank. 2)将该数字乘以给定的等级。 Thus, one specific occurence might give +1, 0 or -1. 因此,一个特定的出现可能会给出+1,0或-1。
  • 3) Which should result in rowSums() (?) over the columns in question. 3)哪个应该在所rowSums()的列上产生rowSums() (?)。

Example : first 4 columns, 4th row: 示例 :前4列,第4行:

Bewustwording (1x +1) + Confrontatie (2x -1) + Coordinerend (17x +1 ) would equal 1 - 2 + 17 = 18 Bewustwording (1x +1)+ Confrontatie (2x -1)+ Coordinerend (17x +1)等于1 - 2 + 17 = 18

       Bewustwording Confrontatie Confrontatie.Outside Coordinerend Delegerend Goedaardig Grappig
1              1            0                    0            1          6          3       0
2              0            1                    0            3          3          0       1
3              1            0                    0            6          2          5       0
4              1            2                    0           17         22          4       0
5              0            0                    0            2          0          0       0
6              0            0                    0            4          9          7       2
7              0            0                    0           10          6          3       0
8              0            1                    0            6          1          2       1
9              1            1                    0           14         15          9       1
10             1            2                    0            9         11          1       1

We want to use this positive/negative attributing in order to ascertain if a given form of communication is more present than other situations. 我们希望使用这种积极/消极的归因,以确定某种形式的沟通是否比其他情况更为明显。 Quite basic, but would allow for more interesting hypotheses to continue on since we are working with a lot of different groups (or subsets). 相当基本,但由于我们正在与许多不同的组(或子集)合作,因此可以继续使用更有趣的假设。

Ideally I would slap this list on the (or any) data which would sprout a new column with a new value (which is 18 in the example above). 理想情况下,我会在(或任何)数据上打击此列表,这些数据将使用新值(在上面的示例中为18)发芽新列。 Sometimes a ranking value might change or has to be corrected, applying the changes should not take too much effort. 有时排名值可能会更改或必须更正,应用更改不应该花费太多精力。 I will probably not be doing this after this is done, hence the easy way for others. 完成后我可能不会这样做,因此对其他人来说很简单。 However, still clueless on how to :) 但是,仍然无法如何:)

> rank.list
                 Action rank
1         Bewustwording    1
2          Confrontatie   -1
3  Confrontatie.Outside   -1
4          Coordinerend    1
5            Delegerend    1
6            Goedaardig    1
7               Grappig    1
8              Hofmaken    1
9           Instruerend    1
10         Onderwijzend    1
11           Ontbindend    0
12              Protest   -1
13             Reactief    0
14     Respons.Negatief   -1
15     Respons.Neutraal    0
16     Respons.Positief    1
17             Sign-out    0
18             Time-out    0
19             Volgzaam    1
20              Vragend    1

Output: ideally output such as Ranking (for the first 2 rows) 输出:理想输出如排名 (前2行)

       Bewustwording Confrontatie Confrontatie.Outside Coordinerend   Ranking
1              1            0                    0            1          2            
2              0            1                    0            3          2        

One dplyr and tidyr possibility could be: 一个dplyrtidyr可能性可能是:

df %>%
 rowid_to_column() %>%
 gather(var, val, -rowid) %>%
 left_join(rank.list, by = c("var" = "Action")) %>%
 mutate(val = val * rank) %>%
 select(-rank) %>%
 group_by(rowid) %>%
 mutate(Row_sum = sum(val),
        Ranking = sum(sign(val))) %>%
 spread(var, val) %>%
 ungroup() %>%
 select(-rowid) 

   Row_sum Ranking Bewustwording Confrontatie Confrontatie.Outside Coordinerend Delegerend Goedaardig Grappig
     <int>   <dbl>         <int>        <int>                <int>        <int>      <int>      <int>   <int>
 1      11       4             1            0                    0            1          6          3       0
 2       6       2             0           -1                    0            3          3          0       1
 3      14       4             1            0                    0            6          2          5       0
 4      42       3             1           -2                    0           17         22          4       0
 5       2       1             0            0                    0            2          0          0       0
 6      22       4             0            0                    0            4          9          7       2
 7      19       3             0            0                    0           10          6          3       0
 8       9       3             0           -1                    0            6          1          2       1
 9      39       4             1           -1                    0           14         15          9       1
10      21       4             1           -2                    0            9         11          1       1

If you want to preserve the original values: 如果要保留原始值:

df %>%
 rowid_to_column() %>%
 gather(var, val, -rowid) %>%
 left_join(rank.list, by = c("var" = "Action")) %>%
 group_by(rowid) %>%
 mutate(Row_sum = sum(val * rank),
        Ranking = sum(sign(val * rank))) %>%
 select(-rank) %>%
 spread(var, val) %>%
 ungroup() %>%
 select(-rowid) 

   Row_sum Ranking Bewustwording Confrontatie Confrontatie.Outside Coordinerend Delegerend Goedaardig Grappig
     <int>   <dbl>         <int>        <int>                <int>        <int>      <int>      <int>   <int>
 1      11       4             1            0                    0            1          6          3       0
 2       6       2             0            1                    0            3          3          0       1
 3      14       4             1            0                    0            6          2          5       0
 4      42       3             1            2                    0           17         22          4       0
 5       2       1             0            0                    0            2          0          0       0
 6      22       4             0            0                    0            4          9          7       2
 7      19       3             0            0                    0           10          6          3       0
 8       9       3             0            1                    0            6          1          2       1
 9      39       4             1            1                    0           14         15          9       1
10      21       4             1            2                    0            9         11          1       1

Given that we are calculating weighted sums across rows, a straightforward apply might suffice: 鉴于我们正在计算跨行的加权和,一个简单的apply可能就足够了:

## weighted sums by rows
dat$Ranking <- apply(dat, 1, function(x, weight) sum(weight * x), weight = rank.list$rank)
dat
#>    Bewustwording Confrontatie Confrontatie.Outside Coordinerend Delegerend
#> 1              1            0                    0            1          6
#> 2              0            1                    0            3          3
#> 3              1            0                    0            6          2
#> 4              1            2                    0           17         22
#> 5              0            0                    0            2          0
#> 6              0            0                    0            4          9
#> 7              0            0                    0           10          6
#> 8              0            1                    0            6          1
#> 9              1            1                    0           14         15
#> 10             1            2                    0            9         11
#>    Goedaardig Grappig Ranking
#> 1           3       0      11
#> 2           0       1       6
#> 3           5       0      14
#> 4           4       0      42
#> 5           0       0       2
#> 6           7       2      22
#> 7           3       0      19
#> 8           2       1       9
#> 9           9       1      39
#> 10          1       1      21

Alternatively, obtain the Ranking column by taking a matrix product between the data.frame and the weight vector: 或者,通过在data.frame和权重向量之间获取矩阵乘积来获取“ Ranking列:

as.matrix(dat) %*% rank.list$rank
#>       [,1]
#>  [1,]   11
#>  [2,]    6
#>  [3,]   14
#>  [4,]   42
#>  [5,]    2
#>  [6,]   22
#>  [7,]   19
#>  [8,]    9
#>  [9,]   39
#> [10,]   21

Data 数据

NB: since the data.frame doesn't contain all columns listed in rank.list only the first few rows of rank.list are used. 注:由于data.frame不包含在列出的所有列rank.list只有前几排rank.list使用。

## data
dat <- structure(list(Bewustwording = c(1L, 0L, 1L, 1L, 0L, 0L, 0L, 
0L, 1L, 1L), Confrontatie = c(0L, 1L, 0L, 2L, 0L, 0L, 0L, 1L, 
1L, 2L), Confrontatie.Outside = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L), Coordinerend = c(1L, 3L, 6L, 17L, 2L, 4L, 10L, 6L, 
14L, 9L), Delegerend = c(6L, 3L, 2L, 22L, 0L, 9L, 6L, 1L, 15L, 
11L), Goedaardig = c(3L, 0L, 5L, 4L, 0L, 7L, 3L, 2L, 9L, 1L), 
    Grappig = c(0L, 1L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 1L), Ranking = c(11L, 
    6L, 14L, 42L, 2L, 22L, 19L, 9L, 39L, 21L)), row.names = c(NA, 
-10L), class = "data.frame")

## weights
rank.list <- structure(list(Action = c("Bewustwording", "Confrontatie", "Confrontatie.Outside", 
"Coordinerend", "Delegerend", "Goedaardig", "Grappig"), rank = c(1L, 
-1L, -1L, 1L, 1L, 1L, 1L)), row.names = c(NA, 7L), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM