-1

My data set consists of the columns CODE, FIRE, CARBON, and DEPTH example of data in the column CODE are chic, chiq, lopc, lopq etc.

Besides this I got information in which is mentioned that each value, chic for example, in the column CODES belongs to either A, B or C.

So I want to create in R a new column which is based on the CODE column. So that when I say to R that when CODE is for example chic, the value in the new column is A on the same row.

I hope someone could help me with this,

Thanks in advance!

( I wanted to insert a column in here with an example of the data, but it didnt work, I hope it is now also clear what I want to create)

Jordy
  • 1
  • 2
  • what you tried til now? – HaveNoDisplayName Nov 26 '14 at 14:31
  • Please take the time to create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. This will make it much easier to help you. – MrFlick Nov 26 '14 at 14:42

2 Answers2

0

You can try :

yourdata$newcolumn<-sapply(as.character(yourdata$CODE),
                           switch,
                           "chic"="A","code2"="B","code3"="C")

Of course, you'll need to adapt the code to your data but you didn't give a lot of details...

Example :

with yourdata the below data.frame :

   CODE FIRE CARBON      DEPTH
1  chic    1      3 -1.3107045
2 code2    1      3  1.0781249
3 code3    1      3 -0.6762211
4  chic    1      3  0.9196376
5 code2    2      4 -0.7161380
6 code2    2      4 -0.5506980
7  chic    2      4  0.3323771
8  chic    2      4 -0.4131832

you get the new data.frame yourdata :

   CODE FIRE CARBON      DEPTH newcolumn
1  chic    1      3 -1.3107045         A
2 code2    1      3  1.0781249         B
3 code3    1      3 -0.6762211         C
4  chic    1      3  0.9196376         A
5 code2    2      4 -0.7161380         B
6 code2    2      4 -0.5506980         B
7  chic    2      4  0.3323771         A
8  chic    2      4 -0.4131832         A
Cath
  • 23,906
  • 5
  • 52
  • 86
  • thank you! that is the output I want to create! only when I apply it to my data frame, I get the following error: In FUN(X[[174L]], ...) : EXPR is a "factor", treated as integer. – Jordy Nov 26 '14 at 15:18
  • @Jordy, can you provide your data (with `dput(yourdata)`) ? It's really hard to reproduce an error without the data... – Cath Nov 26 '14 at 15:29
  • @Jordy, but you can try my function, with `sapply(as.character(yourdata$CODE),...)` and that will probably work if the problem is indeed that CODE is a factor (I edited my answer to modify the call) – Cath Nov 26 '14 at 15:31
0

You could also use (assuming your data is a in a dataframe df)...

df$NEW[df$CODE == "chic"] <- "A"
df$NEW[df$CODE == "chiq"] <- "B"

df
#   CODE DEPTH CARBON FIRE  NEW
# 1 chic     0   24.2    U    A
# 2 chic    10   25.6    U    A
# 3 chiq     0   20.3    B    B
# 4 chiq     0   50.3    B    B
# 5 chiq    10   40.5    B    B
# 6 chiq    20   24.2    B    B
# 7 polc     0   60.5    U <NA>
# 8 polc    10   10.2    U <NA>
# 9 polq     0   20.5    U <NA>

etc...

To recode multiple values at once, use %in% and a vector for example:

df$NEW[df$CODE %in% c("chic", "chiq")] <- "A"

JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • Thank you for helping! I tried your method, only I get the following error message: Error in carbon2$NEW[carbon2$CODE == "ACHIQ", "ACHIC", "INKAQ", "INKAC", : incorrect number of subscripts – Jordy Nov 26 '14 at 15:05
  • It looks like your code is incomplete in your comment. Are you trying to recode multiple values at the same time? – JasonAizkalns Nov 26 '14 at 15:18
  • Yes, example of what i did is, carbon2$NEW[carbon2$CODE == "ALFQ","ALFC","SUNCHQ"] <- "A" carbon2$NEW[carbon2$CODE == "CHALLQ", "CHALLC", "LAGQ"] <- "B" carbon2$NEW[carbon2$CODE == "ACHIQ", "ACHIC", "INKAQ"] <- "C" – Jordy Nov 26 '14 at 15:20
  • To do that you will need to use `%in%` and pass a vector. So, `carbon2$NEW[carbon2$CODE %in% c("ALFQ", "ALFC", "SUNCHQ")] <- "A"` – JasonAizkalns Nov 26 '14 at 15:27