Issue I am trying to write a function using R data.table syntax. It has results if I just run this line countbyYr = data[, .(counts = uniqueN(Col_A)), by = .(Year)], but once I put it into a function (shown below),
Continue readingTag: data.table
[SOLVED] data.table fread, how to read in csv file last N rows
Issue Hі! Can someone show on the example of iris.csv how to load the last 10 rows with the fread function from the data.table thanks udp===== write.csv(iris,"C:\\Users\\TARAS\\Desktop\\iris.csv") data.table::fread(cmd="tail -10 C:/Users/TARAS/Desktop/iris.csv") "tail" Ґ пў«пҐвбп ўгв॥© Ё«Ё ўҐиҐ© Є®¬ ¤®©, ЁбЇ®«пҐ¬®© Їа®Ја
Continue reading[SOLVED] flatten a named list in R – converting the name into first column
Issue Say, I have a named list: library(dplyr) myData <- mtcars %>% group_by(cyl) %>% group_map(~ head(.x, 1L)) names(myData) <- c("Mazda", "Honda", "Suzuki") Now, I want to convert the list into a table looking like this: name mpg disp hp drat
Continue reading[SOLVED] I'd like to put multiples repetition of a list in data.table
Issue I’m trying to create this table: library(data.table) table <- data.table( x = "D" , y = list(c("a", "b"), c("a", "b"),c("a", "b"),c("a", "b"), "test")) table I’d like to use rep() with maybe list(): table <- data.table( x = "D" ,
Continue reading[SOLVED] Aggregate character string into vector in R
Issue I have a data table test: id key 1 2365 1 2365 1 3709 2 6734 2 1908 2 4523 I want to aggregate unique key values by id into vector using data.table package. Expected output: id key_array 1
Continue reading[SOLVED] Find common values of two lists in R data table
Issue I have a data table test: id array1 array2 1 c(1, 2, 3, 4, 5) c(3, 4, 5) 2 c(6, 7, 8, 9, 10) c(6, 7, 0) > str(test) Classes ‘data.table’ and ‘data.frame’: 2 obs. of 3 variables: $
Continue reading[SOLVED] R code to randomly allocate data based on probability/proportion
Issue I’m still relatively new to R and am having trouble figuring out how to randomly allocate data based on a known proportion. Or, if there is another way to crosswalk this data – I’d love to learn that, also.
Continue reading[SOLVED] Overriding data.table key order causes incorrect merge results
Issue In the following example I use a dplyr::arrange on a data.table with a key. This overrides the sort on that column: x <- data.table(a = sample(1000:1100), b = sample(c("A", NA, "B", "C", "D"), replace = TRUE), c = letters)
Continue reading[SOLVED] R Compare Lists and Identify Individual Changes
Issue Here’s an ice cream shop data frame in R, that shows the flavors at a store month to month. df <- data.frame(date = as.Date(c(rep("2022-01-01", 3), rep("2022-02-01", 3), rep("2022-03-01", 4))), flavor = c("Almond", "Apple", "Apricot", "Almond", "Maple", "Mint", "Almond", "Maple",
Continue reading[SOLVED] R How to mutate a subset of rows
Issue I am having trouble mutating a subset of rows in dplyr. I am using the chaining command: %>% to say: data <- data %>% filter(ColA == “ABC”) %>% mutate(ColB = “XXXX”) This works fine but the problems is that
Continue reading