How to filter a list of dataframes based on a unique count of categorical factors in each dataframe?

Advertisements I have a dataframe that I split into a list of dataframes based on a categorical variable in the dataframe: list <- split(mpg, mpg$manufacturer) I want to filter the list to only include dataframes where one of the categorical columns in each dataframe contain at least 5 unique factors, and remove those with less… Read More How to filter a list of dataframes based on a unique count of categorical factors in each dataframe?

data frame rows separated by semicolon, how do I subtract numbers and add to new column?

Advertisements I have a data frame where my rows in one column have numbers separated by a semicolon. How do I subtract the first number from the last number and add that value to a new column? Say my data frame looks like this below: df <- data.frame(x=c("1;2;3","1;1;1;1"),y=c(5,5)) How do I add a new column… Read More data frame rows separated by semicolon, how do I subtract numbers and add to new column?

data.table lappy convert date from numeric yyyymmdd to date format

Advertisements Given the following DT: Date1 <- c(20210103, 20220805, 20191231) Amount <- c(10,20,30) Date2 <- c(20190101, 20180220, 20240930) dt_1 <- data.table(Date1, Amount, Date2) dt_1 I am trying to convert the numeric yyymmdd to date format: date_cols <- c(Date1, Date2) dt_1 <- dt_1[, (date_cols) := lapply(.SD, as.Date), .SDcols = date_cols] can you help me to fix… Read More data.table lappy convert date from numeric yyyymmdd to date format

Rename single column in all dataframes in a list using a vector for the column names

Advertisements I currently have a list of data frames and I want to rename the first column of each of them, using a vector. I have tried the following: cols <- c("list", "of", "names") list <- lapply(list, function(x){ colnames(x)[1] <- cols; x}) But that has resulted in the first column of each dataframe in the… Read More Rename single column in all dataframes in a list using a vector for the column names

How can I assign the computed mean from lapply as a new column in each dataframe of a list in R?

Advertisements I have a list of dataframes. For each dataframe within the list I would like to compute the weighted mean. Henceforth, I want to add a new column into each dataframe within the list called Mean. I want the new columns to repeat the computed mean value across all rows of each dataframe while… Read More How can I assign the computed mean from lapply as a new column in each dataframe of a list in R?

Rename elements of a list from a vector file [R]

Advertisements I have here a list: df.list <- list(structure(list(time = c("2023-02-14 14:33:13", "2023-02-14 14:33:13" ), lat = c(50.085588, 50.0855878333333), lon = c(4.55540016666667, 4.5554), alt = c(195.6, 195.6)), row.names = 1:2, class = "data.frame"), structure(list(time = c("2023-02-14 13:29:21", "2023-02-14 13:29:22" ), lat = c(50.0855996666667, 50.0855998333333), lon = c(4.55512916666667, 4.555129), alt = c(195.7, 195.7)), row.names = 1:2,… Read More Rename elements of a list from a vector file [R]