Canonical Tidyverse Method to Update Some Values of a Vector from a Look-Up Table

Replace values in a dataframe based on lookup table

You posted an approach in your question which was not bad. Here's a smiliar approach:

new <- df  # create a copy of df
# using lapply, loop over columns and match values to the look up table. store in "new".
new[] <- lapply(df, function(x) look$class[match(x, look$pet)])

An alternative approach which will be faster is:

new <- df
new[] <- look$class[match(unlist(df), look$pet)]

Note that I use empty brackets ([]) in both cases to keep the structure of new as it was (a data.frame).

(I'm using df instead of table and look instead of lookup in my answer)

Partially replace grouped data in one dataframe with data from other dataframe

What do you think of this method?:

df1 %>% 
group_by(File) %>%
mutate(rn = row_number()) %>%
rows_update(df2 %>% mutate(rn = row_number()), by = c("File", "rn")) %>%
select(-rn)
# A tibble: 6 × 5
# Groups: File [4]
Utt File x y z
<chr> <chr> <int> <chr> <dbl>
1 xyzxyz F01 1 A 0.451
2 hi there F02 2 B 1.66
3 how are you? F02 3 C 0.505
4 xxxxx F03 4 D 0.757
5 yyzyzyz F03 5 E 1.28
6 hybsfc F12 6 F 0.226

Convert integers to vector in R using Tidyverse

In a tidyverse-like pipeline, you can use the recode function inside a mutate statement

library(dplyr)

df %>%
mutate(your_int_var = recode(your_int_var, `0` = 'no', `1` = 'slight', `2` = 'severe'))

or even better using unquote splicing !!!

values <- c(`0` = 'no', `1` = 'slight', `2` = 'severe')

df %>%
mutate(your_int_var = recode(your_int_var, !!!values))

How to create a consecutive group number

Try Data$number <- as.numeric(as.factor(Data$site))

On a sidenote : the difference between the solution of me and @Chase on one hand, and the one of @DWin on the other, is the ordering of the numbers. Both as.factor and factor will automatically sort the levels, whereas that doesn't happen in the solution of @DWin :

Dat <- data.frame(site = rep(c(1,8,4), each = 3), score = runif(9))

Dat$number <- as.numeric(factor(Dat$site))
Dat$sitenum <- match(Dat$site, unique(Dat$site) )

Gives

> Dat
site score number sitenum
1 1 0.7377561 1 1
2 1 0.3131139 1 1
3 1 0.7862290 1 1
4 8 0.4480387 3 2
5 8 0.3873210 3 2
6 8 0.8778102 3 2
7 4 0.6916340 2 3
8 4 0.3033787 2 3
9 4 0.6552808 2 3

R data table: update join

It is an update join:

library(data.table)
X <- data.table(id = 1:5, L = letters[1:5])
Y <- data.table(id = 3:5, L = c(NA, "g", "h"), N = c(10, NA, 12))
X[Y, on=.(id), c("L", "N"):=.(i.L, i.N)][]
# id L N
# 1: 1 a NA
# 2: 2 b NA
# 3: 3 NA 10
# 4: 4 g NA
# 5: 5 h 12

gives you the desired result.

Here I found a solution for multiple columns:

library(data.table)
X <- data.table(id = 1:5, L = letters[1:5])
Y <- data.table(id = 3:5, L = c(NA, "g", "h"), N = c(10, NA, 12))

X[Y, on=.(id), names(Y)[-1]:=mget(paste0("i.", names(Y)[-1]))]

Another variant:

n <- names(Y)
X[Y, on=.(id), (n):=mget(paste0("i.", n))]


Related Topics



Leave a reply



Submit