How to change column in matrix according to another column

Macaronnos

Let's say I have such matrix:

   Property     Group  Numb
1      rule     panic    13
2      rule      calm    70
3      mild     panic    21
4      mild      calm    59
5     chaos     panic    46
6     chaos      calm    43

I want to change the third column: each value is to be divided by the sum of numbers in this column with corresponding group.

For example, first row should be like that:

1 rule panic 13/(13+21+46)

We divide the value by sum of Numbs for entries with Group = panic. I know the brute force of doing it, but would you help me with an elegant way of doing it?

akrun

Try

library(dplyr)
df1 %>%
    group_by(Group) %>% 
    mutate(Numb = Numb/sum(Numb))

Or

library(data.table)
setDT(df1)[, Numb1:= Numb/sum(Numb) , Group][]

Or using base R

df1$Numb <- with(df1, ave(Numb, Group, FUN=function(x) x/sum(x)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to select column conditionally according to another one in MySQL

How to find the top 5 values of a column according to another column?

How to select a column element according to the size of another column elements?

How to multiply a matrix by a column in another data set?

How to change column values according to size

How to change column name according to group in a column in pandas

How to change column name according to another dataframe in R?

Populate a CSV column according to the value of another column

How to create a column from another df according to matching columns?

Rank according to one column and grouby another column

Using dplyr in order to change column value according to another df

How to SQL ORDER BY but keep the results clumped according to another column?

MySql - Increment according to another column

Change the names in the column of matrix according to the other matrix

Excel: Summing a column according to values in another column

Grouping elements of one column of matrix according to values of another column into cell array

How to sort a file according to a column in another file?

How to reset id column according to another column

Fill one column of vsfgird according to another column

How to set a column value according to another column value Trigger statement?

How to look up a value according to percentile of another column

How to use cursors to update some columns according to another column?

Change value within a df1 column according to another df

how to sort a dataframe according to another dataframe column values?

How to assign a python matrix based on the product of the row and column of another matrix?

How to add prefix to column name according to data in another column

Bigquery : How to retrieve data for a column according to another column's value

How can I change zero matrix to one, according to the predetermined row and column in python?

How to assign the column of one matrix to another, nalgebra?