简体   繁体   English

我如何重命名或为另一列中的列中的某些观察值指定名称

[英]how do i rename or assign name to some observations in a column in another column

I am trying to regroup some rows in a dataset This is my data below我正在尝试重新组合数据集中的一些行这是我下面的数据

Status地位 Count数数 Value价值
Ambi安比 20 20 ₦4,850 ₦4,850
Area区域 11 11 ₦7,150 ₦7,150
error1错误1 461 461 ₦106,909 ₦106,909
error2错误2 659 659 ₦224,723 ₦224,723
unuccessful不成功 1 1 ₦100 ₦100
not executed未执行 1 1 ₦200 ₦200
Successfu成功 43 43 ₦4,444 ₦4,444
feed喂养 4 4 ₦2,111 ₦2,111
Cut 10 10 ₦1,500 ₦1,500

I want to Create a column and name it status2 then in this status2 will be the regrouping of Status for example我想创建一个列并将其命名为 status2 然后在此 status2 中将是 Status 的重新组合,例如

Anywhere you see ambi and area in Status column, categorize it as Failed in Status2 column在 Status 列中看到 ambi 和 area 的任何地方,在 Status2 列中将其归类为 Failed

Anywhere you see error1 and error2 in Status column, categorize it as connection error in Status column2在 Status 列中看到 error1 和 error2 的任何地方,将其归类为 Status column2 中的连接错误

Anywhere you see unsuccessful and not_executed in Status column, categorize it as Failure in Status2 column在 Status 列中看到不成功和 not_executed 的任何地方,在 Status2 列中将其归类为失败

and leave the others in the Status column the way they are in the new Status2 column aside the ones i mention to rename their names并将其他人留在 Status 列中,就像他们在新 Status2 列中的方式一样,除了我提到的那些以重命名他们的名字

here is my code My R knowledge is limited这是我的代码 我的 R 知识有限

error>%
      mutate(Status_2 <-ifelse(error$Status %in% c(Ambi,Area,error1,error2,unuccessful,not 
executed),

Im lost i dont know if im on the right part with the code我迷路了,我不知道我的代码是否正确

here is how I want the outcome to look like这是我希望结果的样子

Status地位 Count数数 Value价值 Status2状态2
Ambi安比 20 20 ₦4,850 ₦4,850 Failed失败的
Area区域 11 11 ₦7,150 ₦7,150 Failed失败的
error1错误1 461 461 ₦106,909 ₦106,909 Connection error连接错误
error2错误2 659 659 ₦224,723 ₦224,723 Connection error连接错误
unuccessful不成功 1 1 ₦100 ₦100 Failure失败
not executed未执行 1 1 ₦200 ₦200 Failure失败
Successful成功的 43 43 ₦4,444 ₦4,444 Successful成功的
feed喂养 4 4 ₦2,111 ₦2,111 feed喂养
Cut 10 10 ₦1,500 ₦1,500 Cut

Sample Data样本数据

structure(list(Status = c("Ambi", "Area", "error1", "error2", 
"unuccessful", "not_executed", "Successfu", "feed", "Cut"), Count = c(20L, 
11L, 461L, 659L, 1L, 1L, 43L, 4L, 10L), Value = c("<U+20A6>4,850", 
"<U+20A6>7,150", "<U+20A6>106,909", "<U+20A6>224,723", "<U+20A6>100", 
"<U+20A6>200", "<U+20A6>4,444", "<U+20A6>2,111", "<U+20A6>1,500"
)), class = "data.frame", row.names = c(NA, -9L))

I think you can use the following solution.我认为您可以使用以下解决方案。 There have some typos in your data set that's why some words may appear incorrect.您的数据集中有一些拼写错误,这就是为什么有些词可能看起来不正确的原因。 Here we use case_when , the LHS must evaluate to a logical vector and the RHS must evaluate to the same type of vector.这里我们使用case_when ,LHS 必须评估为逻辑向量,而 RHS 必须评估为相同类型的向量。 Here because our Status_2 vector is of type character so should be the RHS of our case_when function.这里因为我们的Status_2向量是character类型,所以应该是我们case_when function 的 RHS。

library(dplyr)

df %>%
  mutate(Status_2 = case_when(
    Status %in% c("Ambi", "Area") ~ "Failed",
    Status %in% c("error1", "error2") ~ "connection error",
    Status %in% c("unuccessful", "not_executed") ~ "Failure",
    TRUE ~ Status
  ))

# A tibble: 9 x 4
  Status       Count Value    Status_2        
  <chr>        <int> <chr>    <chr>           
1 Ambi            20 <U+20A6>4,850   Failed          
2 Area            11 <U+20A6>7,150   Failed          
3 error1         461 <U+20A6>106,909 connection error
4 error2         659 <U+20A6>224,723 connection error
5 unuccessful      1 <U+20A6>100     Failure         
6 not_executed     1 <U+20A6>200     Failure         
7 Successfu       43 <U+20A6>4,444   Successfu       
8 feed             4 <U+20A6>2,111   feed            
9 Cut             10 <U+20A6>1,500   Cut   

In case you would like to use ifelse :如果您想使用ifelse

df %>%
  mutate(Status_2 = ifelse(Status %in% c("Ambi", "Area"), "Failed", 
                           ifelse(Status %in% c("error1", "error2"), "connection error",
                                  ifelse(Status %in% c("unuccessful", "not_executed"), 
                                         "Failure", Status))))

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

相关问题 如何按数据框中的列名重命名观察结果? - How to rename observations by column name in data frame? For循环将所有非NA观测值重命名为R中的列名 - For loop to rename all non-NA observations to column name in R 如何使用向量重命名R数据框中的列以指示OLD列名? - How do I rename a column in a R dataframe using a vector to indicate the OLD column name? 如何将观测值添加到现有数据框列? - How do I add observations to an existing data frame column? 如何计算每列的观察次数(1s) - How do I count number of observations (1s) per column 如何在 dataframe 中组合多个观察结果以在列中创建列表? - How do I combine multiple observations in a dataframe to create a list in a column? 如何根据某个字符串值重命名列中的某些观察值? - How to Rename Certain Observations in Column Based on Certain String Value? 如何在 R 中用另一个名称重命名同一列中的多个字符串 - How can I rename multiple string in same column with another name in R 将 dataframe 中的一些观察结果按一列排列,其他的按另一列排列 - Arrange some observations in dataframe by one column, and others by another 如何使用 R 的摘要工具 package 中的 ctable 重命名行和列变量名称 output? - How do I rename row & column variable name output using ctable from R's summarytools package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM