Using mapply for indirect addressing in a data frame

With the next two data frames

> d1
  keystr keynum 
1    abc      5
2    def      2
3    def      7 
4    abc      3 

> d2
   HD  2  3  5  7
1 abc  H  I  J  K
2 def  L  M  N  P

I would like to insert a column d1 $ val that uses the row in keystrand the number in keynumas indices in the data frame d2. The result should be:

> d1
  keystr keynum  val
1    abc      5    J
2    def      2    L
3    def      7    P 
4    abc      3    I 

This should be an indirect mapply application. How can I make the code below

d1 <- data.frame("keystr"=c("abc","def","def","abc"), "keynum"=c(5,2,7,3))
d2 <- data.frame("HD"=c("abc","def"),
                 "2"=c("H","L"), "3"=c("I","M"),
                 "5"=c("J","N"), "7"=c("K","P"))
d1$val <- mapply(function(kstr,knum) d2[kstr,knum],
                 d1$keystr, d1$keynum )

access to elements of this (indirect) method?

+4
source share
6 answers

You can change and combine data using the R base:

d1 <- read.table(text = 'keystr keynum 
1    abc      5
2    def      2
3    def      7 
4    abc      3', stringsAsFactors = FALSE)

d2 <- read.table(text = 'HD  2  3  5  7
1 abc  H  I  J  K
2 def  L  M  N  P', stringsAsFactors = FALSE, check.names = FALSE)

d2 <- reshape(d2, idvar = "HD", varying = names(d2)[-1], v.names = "val",
              times = names(d2)[-1], direction = "long")
merge(d1, d2, by.x = c("keystr", "keynum"), by.y = c("HD", "time"))
#>   keystr keynum val
#> 1    abc      3   I
#> 2    abc      5   J
#> 3    def      2   L
#> 4    def      7   P
+1
source

If you cannot use mapply, you can make a connection:

The code:

library(tidyverse)
d1 <- data.frame("keystr"=c("abc","def","def","abc"), "keynum"=c(5,2,7,3))
d2 <- data.frame("HD"=c("abc","def"),
                 "2"=c("H","L"), "3"=c("I","M"),
                 "5"=c("J","N"), "7"=c("K","P"))

d2 %>%
  gather(keynum, value, -HD) %>%
  mutate(keynum = as.numeric(gsub(keynum, pattern = "X", replacement = ""))) %>%
  left_join(y = ., x = d1, by = c("keystr" = "HD", "keynum"))

Conclusion:

  keystr keynum value
1    abc      5     J
2    def      2     L
3    def      7     P
4    abc      3     I
+1
source

, .

library(dplyr)
library(tidyr)

d3 <- d2 %>%
  gather(keynum, letter, -HD) %>%
  mutate(keynum = as.numeric(sub("X", "", keynum)))

d4 <- d1 %>%
  left_join(d3, by = c("keystr" = "HD", "keynum"))
d4
#   keystr keynum letter
# 1    abc      5      J
# 2    def      2      L
# 3    def      7      P
# 4    abc      3      I

DATA

, stringsAsFactors = FALSE .

d1 <- data.frame("keystr"=c("abc","def","def","abc"), "keynum"=c(5,2,7,3),
                 stringsAsFactors = FALSE)
d2 <- data.frame("HD"=c("abc","def"),
                 "2"=c("H","L"), "3"=c("I","M"),
                 "5"=c("J","N"), "7"=c("K","P"),
                 stringsAsFactors = FALSE)
+1

d1, d2 [-1], cbind . , , . , . ( 3 4 R-, on = e 3,4 ):

( m2 <- sapply(d2[ , -1], as.character) )
#------
     2   3   5   7  
[1,] "H" "I" "J" "K"
[2,] "L" "M" "N" "P"

rownames(m2) <- as.character(d2[[1]])
m2
#--------
    2   3   5   7  
abc "H" "I" "J" "K"
def "L" "M" "N" "P"


(d1$val <-  m2[ cbind(as.character(d1[[1]]),as.character(d1[[2]])) ])
[1] "J" "L" "P" "I"

 d1
#--------
  keystr keynum val
1    abc      5   J
2    def      2   L
3    def      7   P
4    abc      3   I

as.character , . data.frames stringsAsFactors=FALSE. , , , .

+1

I think I OPthought correctly that it mapplycould provide him with a direct solution. He is pretty close to a working solution with his approach mapply. You need to fix only the comparison logic to select a row, and then paste0, which will be used to select a column from d2.

d1$val <- mapply(function(x,y)d2[d2$HD==x,paste0("X",y)],d1$keystr, d1$keynum)
d1
#    keystr keynum val
# 1    abc      5   J
# 2    def      2   L
# 3    def      7   P
# 4    abc      3   I
# 
+1
source

Added check.names = False to include data.frame column names starting with numbers. An index with a matrix of cbind()two columns, pairs i, jwill be extracted all at once.

d1 <- data.frame("keystr"=c("abc","def","def","abc"), "keynum"=c(5,2,7,3))
d2 <- data.frame("HD"=c("abc","def"),
                 "2"=c("H","L"), "3"=c("I","M"),
                 "5"=c("J","N"), "7"=c("K","P"), check.names=FALSE)

d1$val <- mapply(function(kstr,knum) d2[cbind(match(kstr, d1$keystr),
                                              match(knum, names(d2)))],
                 d1$keystr,
                 d1$keynum)

  keystr keynum val
1    abc      5   J
2    def      2   L
3    def      7   P
4    abc      3   I
0
source

Source: https://habr.com/ru/post/1695831/


All Articles