0

I have a data.frame:

head(data)
    År                        Namn N_medlemmar
1 1938  Asea-personalens erk. Csk.        8796
2 1938 Bohuslän-Hallands erk. Csk.       73765
3 1938    Dala-Gäleborgs erk. Csk.       87053
4 1938     De dövstummas erk. Csk.         718
5 1938  Eskilstuna stads erk. Csk.       15657
6 1938       Gefle stads erk. Csk.        9316

I want to add a column Codewith unique values conditioned on the Name column above. In example:

    År                        Name N_medlemmar  Code
1 1938   Asea-personalens erk. Csk.        8796  1
2 1938  Bohuslän-Hallands erk. Csk.       73765  2
3 1939  Bohuslän-Hallands erk. Csk.       12392  2
4 1940     Dala-Gäleborgs erk. Csk.       87053  3
5 1941      De dövstummas erk. Csk.         718  4
6 1942   Eskilstuna stads erk. Csk.       15657  5
7 1943   Eskilstuna stads erk. Csk.        9316  5

So the Code column is an unique value conditioned on the Name column above.

dput(data)
structure(list(År = c(1938L, 1938L, 1938L, 1938L, 1938L, 1938L
), Namn = c("Asea-personalens erk. Csk.", "Bohuslän-Hallands erk. Csk.", 
"Dala-Gäleborgs erk. Csk.", "De dövstummas erk. Csk.", "Eskilstuna stads erk. Csk.", 
"Gefle stads erk. Csk."), N_medlemmar = c(8796L, 73765L, 87053L, 
718L, 15657L, 9316L)), .Names = c("År", "Namn", "N_medlemmar"
), class = "data.frame", row.names = c(NA, 6L))

I tried lapply and for loop but didnt suceed... Any suggestions? Best Regards!

1
  • 2
    I am struggling to see how your second data frame is derived from the first one.
    – NPE
    Mar 22, 2013 at 11:05

2 Answers 2

4

I think what you are asking for is

data$Code <- as.integer(as.factor(data$Namn))

Or just keep it as a factor.

2
  • @NPE Great! Thanks! Interesting solution! I accept your solution because it is handsome:) But does anyone have a solution about how to do it with for or apply family loops? Mar 22, 2013 at 11:49
  • @user1665355 I have added as you requested below, two examples of things that you should not do in this situation. Either NPE's or my first solution are more than suited to this task. You will not achieve anything extra using for and apply family here. Mar 22, 2013 at 12:19
2

Or you can do it like this with match...

data$Code <- match( data$Namn , unique(data$Namn))

Edit - regarding for and apply in this situation

Firstly may I ask why you are so keen to use a for or apply loop in this situation? It sounds a lot like you have an assignment that requires you to use a loop? If that is the case you should get extra credit for answering that you refuse to use one when vectorised methods are fully appropriate. If you really, really need to use a loop for some strange reason, one of many examples to achieve this could be:

data$Code <- rep( NA , nrow(data) )
for( i in 1:nrow(data) ){
  data$Code[i] <- match( data$Namn[i] , unique(data$Namn))
}

Do you see why this is inherently worse? Aside from being less efficient you have to do more typing and indexing to achieve the same result. In short, DON'T DO IT!

I also really recommend that you don't do (in this situation)...

data$Code <- unlist( lapply( data[,"Namn"] , function(x){ match( x , unique(data$Namn)) } ) )

Use the vectors Luke.

1
  • Yes, I see, but I just could not get my mind around it...Thanks! Mar 22, 2013 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.