lapply

Submit every similarly named elements of a list of vectors to a function in R

梦想的初衷 提交于 2020-08-05 13:09:35
问题 Below, I'm wondering how to use BASE R function quantile() separately across elements in L that are named EFL and ESL ? Note: this is a toy example, L could contain any number of similarly named elements. foo <- function(X) { X <- as.matrix(X) tab <- table(row(X), factor(X, levels = sort(unique(as.vector(X))))) w <- diag(ncol(tab)) rosum <- rowSums(tab) obs_oc <- tab * (t(w %*% t(tab)) - 1) obs_c <- colSums(obs_oc) max_oc <- tab * (rosum - 1) max_c <- colSums(max_oc) SA <- obs_c / max_c h <-

Submit every similarly named elements of a list of vectors to a function in R

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-05 13:08:09
问题 Below, I'm wondering how to use BASE R function quantile() separately across elements in L that are named EFL and ESL ? Note: this is a toy example, L could contain any number of similarly named elements. foo <- function(X) { X <- as.matrix(X) tab <- table(row(X), factor(X, levels = sort(unique(as.vector(X))))) w <- diag(ncol(tab)) rosum <- rowSums(tab) obs_oc <- tab * (t(w %*% t(tab)) - 1) obs_c <- colSums(obs_oc) max_oc <- tab * (rosum - 1) max_c <- colSums(max_oc) SA <- obs_c / max_c h <-

A set of functions over multiple data frames and merge the outputs in R

假如想象 提交于 2020-07-22 06:07:28
问题 I have multiple data frames (moving temperature of different duration at 130 observation points), and want to generate monthly average for all the data by applying the below code to each data frame - then put the outcome into one data frame. I have been trying to do this with for-loop, but not getting anywhere. I'm relatively new to R and really appreciate if someone could help me get through this. Here is the glimpse of a data frame: head(maxT2016[,1:5]) X X0 X1 X2 X3 1 20160101 26.08987 26

lapply and mutate_all/for loops

泪湿孤枕 提交于 2020-07-16 08:02:18
问题 I have several data frames within a list which I have to modify by normalizing all the data, in all columns (basically, divide each row/column by the sum of the number of that column). After loading all my raw data frames with lapply I want to iterate over all columns to perform such operation (i.e. mutate(df, df$my_column=df$my_column/sum(df$my_column))). My code is: samplelist <- list(df1 = "path to df1", df2 = "path to df2", df3 = "path to df3") samples <- lapply(names(samplelist),function

lapply with boxplots in R

拈花ヽ惹草 提交于 2020-07-09 11:37:11
问题 I am trying to use lapply to create many box plots in R. When I create the box plots using a ggplot function I get the correct output. When I try to pass the box plot function through the lapply using colnames the function does not work as expected. The correct plot and the incorrect plot examples are attached. doPlot = function(var1) { # Create the plot object ggobj = ggplot(wdbc_train, aes(x = diagnosis,y=var1)) + geom_boxplot() # Add some titles and axis labels ggobj = ggobj + ggtitle(var1

lapply with boxplots in R

℡╲_俬逩灬. 提交于 2020-07-09 11:34:20
问题 I am trying to use lapply to create many box plots in R. When I create the box plots using a ggplot function I get the correct output. When I try to pass the box plot function through the lapply using colnames the function does not work as expected. The correct plot and the incorrect plot examples are attached. doPlot = function(var1) { # Create the plot object ggobj = ggplot(wdbc_train, aes(x = diagnosis,y=var1)) + geom_boxplot() # Add some titles and axis labels ggobj = ggobj + ggtitle(var1

Using lapply to make boxplots of a variable list

穿精又带淫゛_ 提交于 2020-06-28 04:44:10
问题 I want this type of boxplot for several y-variables in my dataset: normal boxplot for all irises with Species as x-value. Since I have multiple y variables to plot, I tried to use lapply like this: varlist <- c('Sepal.Length', 'Sepal.Width') plot <- function (varlist) { require(ggplot2) ggplot(data = iris, aes(x=Species, y=varlist))+ geom_boxplot() } lapply(varlist, FUN = plot) I got this plot: with only one iris per plot How can I get normal boxplots using a type of loop (because of several

Get column names with zero variance using dplyr

Deadly 提交于 2020-06-25 21:01:09
问题 I'm trying to find any variables in my data that have zero variance (i.e. constant continuous variables). I figured out how to do it with lapply but I would like to use dplyr as I'm trying to follow tidy data principles. I can create a vector of just the variances using dplyr but its the last step where I find the values not equal to zero and return the variable names that confusing me. Here's the code library(PReMiuM) library(tidyverse) #> ── Attaching packages ──────────────────────────────

Get column names with zero variance using dplyr

懵懂的女人 提交于 2020-06-25 20:58:27
问题 I'm trying to find any variables in my data that have zero variance (i.e. constant continuous variables). I figured out how to do it with lapply but I would like to use dplyr as I'm trying to follow tidy data principles. I can create a vector of just the variances using dplyr but its the last step where I find the values not equal to zero and return the variable names that confusing me. Here's the code library(PReMiuM) library(tidyverse) #> ── Attaching packages ──────────────────────────────

in R dplyr why do I need to ungroup() after I count()?

本秂侑毒 提交于 2020-06-25 09:09:28
问题 When I first started programming in R I would often use dplyr count() . library(tidyverse) mtcars %>% count(cyl) Once I started using apply functions I started running into issues with count() . If I simply added ungroup() to the end of my count() 's the problems would go away. I don't have any particular reproducibles to show. But can somebody explain what the issue likely was, why ungroup() always fixed it, and are there any drawbacks to consistently using ungroup() after every count() , or