Sorry I should have included the r code for the dataframes for ease of
test:

 

input <- rbind(data.frame(item="item 1.1: earnings <sep> item 1.2: w2
<sep>", loc="shelf 1"),

      data.frame(item="item 1.3: deductions <sep>", loc="drawer 1"),

      data.frame(item="item 1.1: earnings <sep>", loc="shelf 2"))

 

lst <- rbind(data.frame(item="item 1.1", cat="A"),data.frame(item="item
1.2", cat="B"),data.frame(item="item 1.3", cat="C"))

 

 

want to get result like:

 

> result

                                         item      loc cat

1 item 1.1: earnings <sep> item 1.2: w2 <sep>  shelf 1  AB

2                        item 1.3: deductions drawer 1   C

3                          item 1.1: earnings  shelf 2   A

 

Thanks,

Richard

 

From: Tan, Richard 
Sent: Wednesday, January 05, 2011 5:55 PM
To: 'r-help@r-project.org'
Subject: categorize a character column

 

Hi, I know I can do this with a for loop with strsplit and grep, but is
there more efficient way?

 

Given a data dataframe (input) and a category column (lst), 

 

> input

                                         item      loc

1 item 1.1: earnings <sep> item 1.2: w2 <sep>  shelf 1

2                        item 1.3: deductions drawer 1

3                          item 1.1: earnings  shelf 2

> lst

      item cat

1 item 1.1   A

2 item 1.2   B

3 item 1.3   C

 

how to get a result frame like 

 

> result

                                         item      loc cat

1 item 1.1: earnings <sep> item 1.2: w2 <sep>  shelf 1  AB

2                        item 1.3: deductions drawer 1   C

3                          item 1.1: earnings  shelf 2   A

 

Thanks,

Richard

 


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to