Populate a new column in a dataframe with a lookup from a double matrix

后端 未结 6 1933
旧时难觅i
旧时难觅i 2021-02-15 20:27

I have a dataframe df:

colour  shape
\'red\'   circle
\'blue\'  square
\'blue\'  circle
\'green\' sphere

And a double matrix m with named rows/

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-15 21:25

    merge() is your friend here. To use it, we need an appropriate data frame to merge with containing the stacked version of your ID matrix. I create that as newdf with the code below:

    df <- data.frame(matrix(1:9, ncol = 3))
    colnames(df) <- c("circle","square","sphere")
    rownames(df) <- c("red","blue","green")
    
    newdf <- cbind.data.frame(ID = unlist(df), 
                              expand.grid(colour = rownames(df), 
                                          shape = colnames(df)))
    

    Which results in:

    > newdf
            ID colour  shape
    circle1  1    red circle
    circle2  2   blue circle
    circle3  3  green circle
    square1  4    red square
    square2  5   blue square
    square3  6  green square
    sphere1  7    red sphere
    sphere2  8   blue sphere
    sphere3  9  green sphere
    

    Then with your original data in object df2, defined using

    df2 <- data.frame(colour = c("red","blue","blue","green"),
                      shape = c("circle","square","circle","sphere"))
    

    use merge()

    > merge(newdf, df2, sort = FALSE)
      colour  shape ID
    1    red circle  1
    2   blue circle  2
    3   blue square  5
    4  green sphere  9
    

    You can store that and rearrange the columns if you need that:

    > res <- merge(newdf, df2, sort = FALSE)
    > res <- res[,c(3,1,2)]
    > res
      ID colour  shape
    1  1    red circle
    2  2   blue circle
    3  5   blue square
    4  9  green sphere
    

提交回复
热议问题