In R language, I want to change the contents of the list according to the corresponding table.

Asked 2 years ago, Updated 2 years ago, 36 views

There is now a list of names (string) you want to convert and a one-to-one correspondence table with that name.
Specifically, the list is

[1]
"Ah" or "sa" or "ta" or "na" is "
[2]
"Ma" and "La" "Wa" "I" "Ki"

Like this, the correspondence table is

Hiragana Kanji
that's   
good doctor   
cormorant   
Picture
Oh
Family...

It looks like .
From here, I would like to use this correspondence table to convert the names on the list.
In other words, for example,

[1]
"A" "family" "difference" "many" "name" "tooth"
[2]
"Ma" "Arrow" etc. "Wa" "Doctor" "Ki"

The goal is to convert the contents of the list in bulk according to the corresponding table as shown in .
I tried the stringr package, but it didn't work...
If stringr works, I would like to ask you how to do it, and if there is a package other than stringr that is appropriate, I would like to ask you how to use it.
I look forward to your kind cooperation.

r

2022-09-30 18:24

1 Answers

The following is how match() + lapply().If the combination is not in the conversion table, it is NA.

trans<-data.frame(
  hiragana=c("a", "i", "u", "e", "o", "ka", "sa", "ta", "na", "ha", "ha", "ha", "ha", "ha",
               "Ma", "ya", "ra", "wa", "i", "ki"),
  kanji=c("A", "Doctor", "Cormorant", "Picture", "Go", "Department", "Difference", "Many", "Name", "Tooth",
            Room, Arrow, etc., Japanese, Doctor, Ki)
)

src<-list(
  c("a", "ka", "sa", "ta", "na", "ha"),
  c("ma", "ya", "la", "wa", "i", "ki")
)

lapply(src, function(x){
  as.vector (trans$kanji [match(x,trans$hiragana)])
})

=>

[[1]]
[1] "A" "family" "difference" "many" "name" "tooth"

[[2]]
[1] "Ma" "Arrow" etc. "Wa" "Doctor" "Ki"


2022-09-30 18:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.