How to create a new column using R which says TRUE/FALSE if another column displays a specific value?

Karima

Here is a simple dataset:

mydf <- tibble(country = rep(c("UK", "US"), each = 3),
               date = rep(c("Day1", "Day2", "Day3"), times = 2),
               cases = seq(1:6))

# A tibble: 6 x 3
  country date  cases
  <chr>   <chr> <int>
1 UK      Day1      1
2 UK      Day2      2
3 UK      Day3      3
4 US      Day1      4
5 US      Day2      5
6 US      Day3      6

I want to create a new column called "part_us" using mutate() where TRUE is displayed if the value in "country" is equal to "US" and FALSE if it's anything other than "US".

It's a simple task but I am still learning R and would be grateful for some help with this.

Duck

You can use ifelse() inside mutate() on your dplyr pipeline:

library(dplyr)
#Data
mydf <- tibble(country = rep(c("UK", "US"), each = 3),
               date = rep(c("Day1", "Day2", "Day3"), times = 2),
               cases = seq(1:6))
#Code
mydf <- mydf %>% mutate(part_us=ifelse(country=='US',T,F))

Output:

# A tibble: 6 x 4
  country date  cases part_us
  <chr>   <chr> <int> <lgl>  
1 UK      Day1      1 FALSE  
2 UK      Day2      2 FALSE  
3 UK      Day3      3 FALSE  
4 US      Day1      4 TRUE   
5 US      Day2      5 TRUE   
6 US      Day3      6 TRUE 

Another way can be next: (Many thanks and credits to @Qwethm):

#Code 2
mydf <- mydf %>% mutate(part_us=country=="US")

Same output.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Create a new column with value in another in pandas

分類Dev

Create new column that carries up the last value of another column, by group

分類Dev

Create new column in R yes/no based off another column

分類Dev

Dataframes in R: creating a new column which contains a value from a second column which is named in a third column

分類Dev

Create column with dplyr based on value and also frequency of another column, in R

分類Dev

Create a new column with date value(2000-05-06) using mutate function in dplyr in R

分類Dev

Create new pd.DataFrame column if value of existing column contains specific substring

分類Dev

Create a new column in a Spark DataFrame using a var with constant value

分類Dev

how to assign new column of values by a specific column?

分類Dev

How to create a new column conditioned on the occurrences of two other columns in R using dplyr?

分類Dev

Select values from one column which share a value in another column

分類Dev

How to create new values in a pandas dataframe column based on values from another column

分類Dev

Create new column based on last 2 digits of values in another column

分類Dev

Create new column based on partial match with another column

分類Dev

Python: create new column and copy value from other row which is a swap of current row

分類Dev

Creating a new variable from the ranges of another column in which the ranges change - R

分類Dev

How to select specific row column pairs in numpy array which have specific value?

分類Dev

How do I create a new column if the text from one column if the text from a second column contains a specific string pattern?

分類Dev

how to subtract the next column by the previous column and create a new column after?

分類Dev

How to add new rows with different value in one column in R

分類Dev

How to add new column depends on row value in R?

分類Dev

Create a new column which shows the last enquiry date based on ID using SQL

分類Dev

How to create a computed column that references another column from another table?

分類Dev

How to get column which is assigned under another column

分類Dev

Create new dataframe column based on other column in R

分類Dev

R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?

分類Dev

How to add new column to existing table and set it value based on existing column using EF migrations

分類Dev

create new column with data in a column

分類Dev

How to assign value to rows which are between two rows with specific string in column in dataframe?

Related 関連記事

  1. 1

    Create a new column with value in another in pandas

  2. 2

    Create new column that carries up the last value of another column, by group

  3. 3

    Create new column in R yes/no based off another column

  4. 4

    Dataframes in R: creating a new column which contains a value from a second column which is named in a third column

  5. 5

    Create column with dplyr based on value and also frequency of another column, in R

  6. 6

    Create a new column with date value(2000-05-06) using mutate function in dplyr in R

  7. 7

    Create new pd.DataFrame column if value of existing column contains specific substring

  8. 8

    Create a new column in a Spark DataFrame using a var with constant value

  9. 9

    how to assign new column of values by a specific column?

  10. 10

    How to create a new column conditioned on the occurrences of two other columns in R using dplyr?

  11. 11

    Select values from one column which share a value in another column

  12. 12

    How to create new values in a pandas dataframe column based on values from another column

  13. 13

    Create new column based on last 2 digits of values in another column

  14. 14

    Create new column based on partial match with another column

  15. 15

    Python: create new column and copy value from other row which is a swap of current row

  16. 16

    Creating a new variable from the ranges of another column in which the ranges change - R

  17. 17

    How to select specific row column pairs in numpy array which have specific value?

  18. 18

    How do I create a new column if the text from one column if the text from a second column contains a specific string pattern?

  19. 19

    how to subtract the next column by the previous column and create a new column after?

  20. 20

    How to add new rows with different value in one column in R

  21. 21

    How to add new column depends on row value in R?

  22. 22

    Create a new column which shows the last enquiry date based on ID using SQL

  23. 23

    How to create a computed column that references another column from another table?

  24. 24

    How to get column which is assigned under another column

  25. 25

    Create new dataframe column based on other column in R

  26. 26

    R: how to use the aggregate()-function to sum data from one column if another column has a distinct value?

  27. 27

    How to add new column to existing table and set it value based on existing column using EF migrations

  28. 28

    create new column with data in a column

  29. 29

    How to assign value to rows which are between two rows with specific string in column in dataframe?

ホットタグ

アーカイブ