简体   繁体   中英

How can I add and populate a new column based on an existing factor column in R?

-I would like to add a Species column and populate that column with either 'deer' or 'cow' depending on the Id, a factor column.

-My animal Ids are either A or B followed by 628-637 for cow and above 80000 for deer (eg, A628, A82117).

-Anything with an Id below 1000, A or B should be classified as 'cow' and everything else 'deer'.

Sample Data

You can try with the following steps:

  • get a substring of the Ids to extrace the numeric part
  • cast the substring as numeric
  • use the numeric value to generate the animal type column

You can do it directly in R (also if ids are represented as a factor column ):

ids <- factor(c('A630','B81000','A1200','B626'))
type <- unlist(lapply(ids, function(x) ifelse(as.numeric(substr(as.character(x), 2, nchar(as.character(x))))<1000, 'cow', 'deer')))
animals <- data.frame(ids=ids, type=factor(type))
is.factor(animals$type)
animals

You can also prepare your data in a database with the following SQL code:

CREATE TABLE ANIMALS
(
 ID VARCHAR(6)
);

INSERT INTO ANIMALS VALUES ('A628');
INSERT INTO ANIMALS VALUES ('A81000');
 
SELECT 
 ID,
 CASE WHEN CAST(SUBSTR(ID,2,LENGTH(ID)-1) as decimal) < 1000 THEN 'COW' ELSE 'DEER' END AS TYPE
FROM ANIMALS A;

You can also create a new table with both columns:

CREATE TABLE CLASSIFIED_ANIMALS AS
SELECT 
  ID,
  CASE WHEN CAST(SUBSTR(ID,2,LENGTH(ID)-1) as decimal) < 1000 THEN 'CAW' ELSE 'DEER' END as TYPE
FROM ANIMALS A;

In dplyr :

df %>%
  mutate(animals = as.numeric(sub("A|B(\\d+).*", "\\1", ids)),
         animals = ifelse(animals > 600 & animals < 80000, "cow", "deer"))
     ids animals
1   A628     cow
2 B82117    deer
3  A1200     cow
4   B626     cow
5 B80007    deer

How this works:

  • first we extract the numerical part of the ids column using backreference \\\\1 and convert it to type numeric
  • then we run a simple ifelse comparison to assign the labels

Data:

df <- data.frame(
  ids = factor(c('A628','B82117','A1200','B626', 'B80007'))
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM