Home > Software engineering >  Is there an R function that can replace a nominal values for certain numeric values within a particu
Is there an R function that can replace a nominal values for certain numeric values within a particu

Time:11-21

given a dataframe below

     A    B    C
A    0    3   1.1
B   3.1   0    .9
C    2    2.1   0

And a key of values below

Label  Num
w      0
x      1
y      2
z      3

how do I generate an output

     A    B    C
A    w    z    x
B    z    w    x
C    y    y    w

CodePudding user response:

data.table approach

library(data.table)
#make them data.tables, keep rownames
setDT(df, keep.rownames = "id")
setDT(df2)
# melt to long format
df.melt <- melt(df, id.vars = "id", variable.name = "id2", variable.factor = FALSE)
# perform rolling join
df.melt[, new_value := df2[df.melt, Label, on = .(Num = value), roll = Inf]]
# cast to wide again
dcast(df.melt, id ~ id2, value.var = "new_value")

   id A B C
1:  A w z x
2:  B z w w
3:  C y y w

sample data

df <- read.table(text = "     A    B    C
A    0    3   1.1
B   3.1   0    .9
C    2    2.1   0", header = TRUE)

df2 <- read.table(text = "Label  Num
w      0
x      1
y      2
z      3", header = TRUE)

CodePudding user response:

Dplyr approach :

library(dplyr)
library(tidyr)

df <- tibble(rownames=c("A", "B", "C"),
       A=c(0, 3.1, 2),
       B=c(3,0,2.1),
       C=c(1.1, 0.9, 0))

df2 <- tibble(label = c("w", "x", "y", "z"),
              values = c(0,1,2,3))

df%>%mutate(across(A:C, round, digit=0)) %>%
     pivot_longer(-1, names_to="colnames", values_to=("values")) %>%
     left_join(df2) %>%
     select(1,2,4) %>% 
     pivot_wider(., 1:3, names_from="colnames", values_from="label")

# A tibble: 3 x 4
  rownames A     B     C    
  <chr>    <chr> <chr> <chr>
1 A        w     z     x    
2 B        z     w     x    
3 C        y     y     w    

First use across to round all values, then pivot_longer to get a "long" format whhere you can left_join the two tables by values. Select to remove the column containing the numbers, then a last pivot_wider to return to original format.

  • Related