简体   繁体   English

如何在新列中基于条件语句添加文本

[英]How to add text based on conditional statement in a new column

In RStudio, I want to add two new columns to a dataframe. 在RStudio中,我想向数据框添加两个新列。 These two columns should be filled with text based on conditional statements from an already existing column in the dataframe. 这两个列应基于数据框中已有的列中的条件语句填充文本。

Data.frame 数据框

Variable is: number c(1,2,3,4, etc.) 变量是:数字c(1,2,3,4等)

I need a code (statement) creating and filling out the two new columns "type_a" and "type_b" based on the number in the "number" column. 我需要一个代码(语句),该代码根据“数字”列中的数字创建并填写两个新列“ type_a”和“ type_b”。 I have to use the exact number, so "if 1 in "number" fill out "type_a" with "some_text1" and fill out "type_b" with "some_text2". Next line will be "if 2 or 3 in "number" fill out "type_a" with "some_text3" and "type_b" with "some_text4". 我必须使用确切的数字,因此“如果“数字”中的1填写“ some_text1”的“ type_a”,然后用“ some_text2”填写“ type_b”,下一行将是“如果数字中的2或3”用“ some_text3”输出“ type_a”,用“ some_text4”输出“ type_b”。

This should be fairly simple, however, I am new to R and currently following courses. 这应该相当简单,但是,我是R的新手,目前正在学习课程。

Thanks, 谢谢,

EDIT: 编辑:

Thanks to the answers below, I have now managed to do what I want with this code. 多亏了以下答案,我现在可以使用此代码完成我想做的事情。 The question now is, can I somehow include the "type_b" statement in the first "case_when" so I dont have to write the list of numbers twice, or is it only possible to include one column per "case_when"? 现在的问题是,我可以以某种方式在第一个“ case_when”中包含“ type_b”语句,这样我就不必两次写数字列表,还是每个“ case_when”仅包含一列? (in SAS I would create the two columns type_a and type_b first and then write "if number in (1,2,4,6) then do; type_a='some_text'; type_b ='some_text2; end;). (在SAS中,我将首先创建两列type_a和type_b,然后输入“如果(1,2,4,6)中的数字,则执行; type_a ='some_text'; type_b ='some_text2; end;)。”

Thanks 谢谢

ds <- tibble(number = 1:6)

ds %>% 
  mutate(
    type_a = case_when(
      number %in% c(1,2,4,6) ~ "some_text", TRUE ~ NA_character_
    ),
    type_b = case_when(
      number %in% c(1,2,4,6) ~ "some_text2", TRUE ~NA_character_
      )
    )

#The code below do not work, but hopefully you get the idea!

library(tidyverse)
ds <- tibble(number = 1:6)

 ds %>% 
  mutate(
    type_a, type_b = case_when(
      number %in% c(1,2,4,6) ~ "some_text", ~ "some_text2", 
TRUE ~ NA_character_))

You can create a data frame with your keys to replace and use the merge function : 您可以使用键创建一个数据框以替换并使用merge功能:

n <- 4
df <- data.frame(number = sample(1:n, 10, replace = TRUE))
df
>   number
1       2
2       2
3       1
4       4
5       4
6       1
7       4
8       3
9       1
10      3


df_text <- data.frame(number = 1:n, text_a = paste0("text", 1:n), text_b = paste0("text", 1:n+n))
df_text
>  number text_a text_b
1      1  text1  text5
2      2  text2  text6
3      3  text3  text7
4      4  text4  text8

merge(df, df_text)
>   number text_a text_b
1       1  text1  text5
2       1  text1  text5
3       1  text1  text5
4       2  text2  text6
5       2  text2  text6
6       3  text3  text7
7       3  text3  text7
8       4  text4  text8
9       4  text4  text8
10      4  text4  text8

You can use sort = FALSE in merge if order matters 如果订单很重要,则可以在merge使用sort = FALSE

If you are just starting out with R, I would recommend looking into "R for data science" ( https://r4ds.had.co.nz/ ) by Hadley Wickham. 如果您刚开始使用R,我建议您研究一下Hadley Wickham撰写的“ R for data science”( https://r4ds.had.co.nz/ )。 Great resource. 巨大的资源。

Here's a solution to what I think you are trying to do: 这是我认为您要尝试执行的操作的解决方案:

library(tidyverse)
ds <- tibble(number = 1:5)

ds %>% 
mutate(
  type_a = case_when(
    number == 1 ~ "some_text1",
    number == 2 | number == 3 ~ "some_text3",
    TRUE ~ NA_character_
  ), 
  type_b = case_when(
    number == 1 ~ "some_text2",
    number == 2 | number == 3 ~ "some_text4",
    TRUE ~ NA_character_
  )
)

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

相关问题 如何基于唯一条件语句在 R 中创建新列? - How to create a new column in R based on a unique conditional statement? 根据另一列 R 的条件语句创建新列 - Create a new column based on conditional statement of another column R 如何根据另一列的条件语句将因子列添加到数据框? - How to add a factor column to dataframe based on a conditional statement from another column? 如何根据具有空白单元格的另一列的条件语句添加一列的值? - How to add values of one column based on conditional statement of another column that has blank cells? 根据条件语句从其他列填充R中的新列 - Populate new column in R from other columns based on conditional statement 基于不同列分配新行字符的条件语句 - Conditional statement to assign new row character based on different column 如何使用基于多列值的条件语句对 dataframe 进行子集化 - How to subset a dataframe with a conditional statement based on multiple column values 如何使用逻辑语句添加新列 - how to add a new column using a logic statement 如何在一列数据中以 NA 为条件添加新列 - How to add new column conditional to NA in one column of data 使用基于if语句的for循环对行进行分组并添加新列 - Group rows with for loop based on if statement and add new column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM