0

I have a problem in replacing codes in a dataframe of 3890 observations. My dataframe has a character variable df$IJN which contains values from 1 to 27 (except 2). I would like to replace these with meaningful data as follow

If(1 OR 6 OR 10 OR 14 OR 18 OR 22 OR 26) should be replaced with UL. If(3 OR 7 OR 11 OR 15 OR 19 OR 23 OR 27) should be replaced with LL. If(4 OR 8 OR 12 OR 16 OR 20 OR 24) should be replaced with UR. If(5 OR 9 OR 13 OR 17 OR 21 OR 25) should be replaced with LR.

(U,L,R,and L Refer to Upper, Lower, Right, and Left sites in the order)

I thought of a for() with if() could not manage with it Also thought of df[which(df=="27")] ="LL" may work one by one not sure! Any help please. R v3.1 - Windows 7
E-H Shabana, Paris.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
Shabana
  • 155
  • 1
  • 6
  • Can you precise how do you replace 2 in your IJN vector? We suspect that you forget this value from your mapping list. – agstudy Jun 26 '13 at 16:23
  • The numbers corresponds to photos. It happened that image 2 was not considered in the study. So there is no fault. – Shabana Jun 26 '13 at 18:32

3 Answers3

2

A solution using the qdap package:

## reformatted your key
codes <- list(UL=c(1, 6, 10, 14, 18, 22, 26), 
    LL = c(3, 7, 11, 15, 19, 23, 27),
    UR = c(4, 8, 12, 16, 20, 24),
    LR = c(5, 9, 13, 17, 21, 25))

## A reproducible example
set.seed(10)
IJN <- c("UR", sample(unlist(codes,,FALSE), 20, TRUE))

## Use lookup and list2df
lookup(IJN, list2df(codes), missing = NULL)

## > IJN
##  [1] "UR" "LL" "LL" "LL" "UR" "UL" "UL" "LL" "LL" "UR"
## [11] "LL" "UR" "UR" "UL" "UR" "LL" "LL" "UL" "UL" "LL"
## [21] "LR"

The last line indexes to find values in the IJN vector that match the key and replaces only them. lookup is an environment hash so it's pretty speedy.

I see you're a new SO user. Welcome. As you learn the norms of the community you'll get better at posting. One thing I'd ask is that you work to devel your question more with a reproducible example. Please use code tags as well.

Community
  • 1
  • 1
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • @agstudy good call on the `set.seed`. No the lookup is not excel inspired it is the product of a need to do simple lookups (1 to 1 correspondence) with large data sets. The people at www.talkstats.com were very helpful in its development. – Tyler Rinker Jun 26 '13 at 16:21
  • I should look at the package qdap.Thanks for your rapide answer. – Shabana Jun 27 '13 at 08:19
1

First, let's make this example minimal and reproducible. Let's say that instead of 27 values of IJN, you have 3. Value 1 gets replaced with "LL", values 2 and 3 get replaced with "UL". I'll put them in a 1-column data frame

d <- data.frame(IJN = round(runif(n=10, min=1, max=3)))

Now, we'll set up a second data frame, with each possible value of IJN exactly once, and its corresponding label in a separate column:

refDF <- data.frame(IJN = 1:3, nm=c("LL", "UL", "UL"))

Finally, merge them into a single data frame:

d2 <- merge(d, refDF, by="IJN")

Note that this doesn't quite answer your question: instead of replacing the values of your column, I added a new column with a different name, with values corresponding to your values. Note that merge also re-orders the rows of your column (it sorts the by column, in this case df$IJN) but this should not be a problem in most applications.

Drew Steen
  • 16,045
  • 12
  • 62
  • 90
  • Thanks for the rapid response. – Shabana Jun 27 '13 at 08:20
  • You're welcome. On stackoverflow, it is polite to choose whichever answer you feel is best, and 'accept' it by clicking the green check mark next to the answer. That signals to other users which answer is most helpful, and rewards the answerer with a few reputation points. – Drew Steen Jun 27 '13 at 15:29
1

You can do it with a for loop and a lot of ifs, but a more idiomatic, vectorised R approach would be to use ifelse:

dj$IJN <- ifelse(dj$IJN %in% c(1, 6, 10, 14, 18, 22, 26), "UL",
          ifelse(dj$IJN %in% c(3, 7, 11, 15, 19, 23, 27), "LL",
          ifelse(dj$IJN %in% c(4, 8, 12, 16, 20, 24, 28), "UR", "LR"))) 
Hong Ooi
  • 56,353
  • 13
  • 134
  • 187