Extract row corresponding to minimum value of a variable by group

Slightly more elegant: library(data.table) DT[ , .SD[which.min(Employees)], by = State] State Company Employees 1: AK D 24 2: RI E 19 Slighly less elegant than using .SD, but a bit faster (for data with many groups): DT[DT[ , .I[which.min(Employees)], by = State]$V1] Also, just replace the expression which.min(Employees) with Employees == min(Employees), if your data … Read more

Getting the top values by group

From dplyr 1.0.0, “slice_min() and slice_max() select the rows with the minimum or maximum values of a variable, taking over from the confusing top_n().“ d %>% group_by(grp) %>% slice_max(order_by = x, n = 5) # # A tibble: 15 x 2 # # Groups: grp [3] # x grp # <dbl> <fct> # 1 0.994 … Read more

Cleaning `Inf` values from an R dataframe

Option 1 Use the fact that a data.frame is a list of columns, then use do.call to recreate a data.frame. do.call(data.frame,lapply(DT, function(x) replace(x, is.infinite(x),NA))) Option 2 — data.table You could use data.table and set. This avoids some internal copying. DT <- data.table(dat) invisible(lapply(names(DT),function(.name) set(DT, which(is.infinite(DT[[.name]])), j = .name,value =NA))) Or using column numbers (possibly faster … Read more

Convert column classes in data.table

For a single column: dtnew <- dt[, Quarter:=as.character(Quarter)] str(dtnew) Classes ‘data.table’ and ‘data.frame’: 10 obs. of 3 variables: $ ID : Factor w/ 2 levels “A”,”B”: 1 1 1 1 1 2 2 2 2 2 $ Quarter: chr “1” “2” “3” “4” … $ value : num -0.838 0.146 -1.059 -1.197 0.282 … Using … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)