Adding a column of factors based on other categorical values

Constantin

I have a massive dataset, and I want to add a factor to each value based on another factor. Currently, my data look like this:

     Type      Value
 1   Wild      68.51
 2   Wild      91.94
 3   Captive   72.58
 4   Hybrid    85.38

But I want to add another column of factors - {Australia, Costa Rica, Brazil} - that is based on if animals are wild, captive or hybrids. The data frame should then look like this:

     Type      Value    Status
 1   Wild      68.51    Costa Rica
 2   Wild      91.94    Costa Rica
 3   Captive   72.58    Australia
 4   Hybrid    85.38    Brazil 
Maurits Evers

Something like this using dplyr::case_when?

library(dplyr);
df %>%
    mutate(Status = case_when(
        Type == "Wild" ~ "Costa Rica",
        Type == "Captive" ~ "Australia",
        Type == "Hybrid" ~ "Brazil"));
#     Type Value     Status
#1    Wild 68.51 Costa Rica
#2    Wild 91.94 Costa Rica
#3 Captive 72.58  Australia
#4  Hybrid 85.38     Brazil

Sample data

df <- read.table(text =
    "Type      Value
    1   Wild      68.51
    2   Wild      91.94
    3   Captive   72.58
    4   Hybrid    85.38", header = T)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Edit column values based on other column values

From Dev

Sum the number of values in one column, depending on factors in two other columns

From Dev

Adding all the values of the column based on column Header

From Dev

Adding column data based on column values?

From Dev

Sorting a pandas dataframe based on number of values of a categorical column

From Dev

Percentages of "categorical values" in one column based on subset of data frame R

From Dev

select column values based on other column date

From Dev

Multiply values in column based on other column

From Dev

select column values based on other column date

From Dev

Multiply values in column based on other column

From Dev

Filling in NA values based on other values in the column

From Dev

Bash - adding values in row based on column

From Dev

Adding values for missing rows based on a column value

From Dev

How to filter based on column values in other rows?

From Dev

Update a column based on other values in the row

From Dev

Find values in One Column based on Other Columns

From Dev

Dropping duplicates based on other column values (Python)

From Dev

Return the occurance of a value in a column based on other values

From Dev

Sum of column values based on other columns in R

From Dev

TSQL Aggregating values based on other column

From Dev

Adding Column to pandas DataFrame in Vectorized way conditioning on other column values

From Dev

Adding column values as long as a value in other column matches

From Dev

Pandas change values in column based on values in other column

From Dev

How to sort the values in one column based on other column values?

From Dev

comparing column values based on other column values in pandas

From Dev

Sum values in a column based on conditions in other column and also change values

From Dev

Select the range of values in the column based on the interval of values in the other column in pandas

From Dev

Adding the values of second column based on date and time of first column

From Dev

Adding the values of second column based on date and time of first column

Related Related

  1. 1

    Edit column values based on other column values

  2. 2

    Sum the number of values in one column, depending on factors in two other columns

  3. 3

    Adding all the values of the column based on column Header

  4. 4

    Adding column data based on column values?

  5. 5

    Sorting a pandas dataframe based on number of values of a categorical column

  6. 6

    Percentages of "categorical values" in one column based on subset of data frame R

  7. 7

    select column values based on other column date

  8. 8

    Multiply values in column based on other column

  9. 9

    select column values based on other column date

  10. 10

    Multiply values in column based on other column

  11. 11

    Filling in NA values based on other values in the column

  12. 12

    Bash - adding values in row based on column

  13. 13

    Adding values for missing rows based on a column value

  14. 14

    How to filter based on column values in other rows?

  15. 15

    Update a column based on other values in the row

  16. 16

    Find values in One Column based on Other Columns

  17. 17

    Dropping duplicates based on other column values (Python)

  18. 18

    Return the occurance of a value in a column based on other values

  19. 19

    Sum of column values based on other columns in R

  20. 20

    TSQL Aggregating values based on other column

  21. 21

    Adding Column to pandas DataFrame in Vectorized way conditioning on other column values

  22. 22

    Adding column values as long as a value in other column matches

  23. 23

    Pandas change values in column based on values in other column

  24. 24

    How to sort the values in one column based on other column values?

  25. 25

    comparing column values based on other column values in pandas

  26. 26

    Sum values in a column based on conditions in other column and also change values

  27. 27

    Select the range of values in the column based on the interval of values in the other column in pandas

  28. 28

    Adding the values of second column based on date and time of first column

  29. 29

    Adding the values of second column based on date and time of first column

HotTag

Archive