lapply

How can I do 3064 regressions using the lapply function

混江龙づ霸主 提交于 2020-03-25 05:53:13
问题 Hi i am starting to use r and am stuck on analyzing my data. I have a dataframe that has 157 columns. Column 1 is the dependent variable and from column 2 to 157 they are the independent variables, but from column 2 to column 79 it is a type of independent variable (n = 78) and from 80 to 157 another type (n = 78). I want to perform (78 x 78 = 6084) multiple linear regressions leaving the first independent variable of the model fixed one at a time, from columns 2 to 79. I can fix the

using an apply function with ggplot2 to create bar plots for more than one variable in a data.frame

帅比萌擦擦* 提交于 2020-02-25 05:20:14
问题 Is there a way to use an apply function in R in order to create barplots with ggplot2 ? Say, we have a dataframe containing only factor variables out of which one is boolean. In my case I have a dateframe with +40 variables. Can one plot all the variables against the boolean one with a single line of code? data("diamonds") factors <- sapply(diamonds, function(x) is.factor(x)) factors_only <- diamonds[,factors] factors_only$binary <- sample(c(1, 0), length(factors_only), replace=TRUE) factors

Apply an already defined function to all dataframes at once

旧城冷巷雨未停 提交于 2020-02-22 08:10:15
问题 I already have defined a function (which works fine). Nevertheless, I have 20 dataframes in the working space to which I want to lapply the same function (dat1 to dat20). So far it looks like this: dat1 <- func(dat=dat1) dat2 <- func(dat=dat2) dat3 <- func(dat=dat3) dat4 <- func(dat=dat4) ... dat20 <- func(dat=dat20) However, is there a way to do this more elegant with a shorter command, i.e. to lapply the function to all dataframes at once? I tried this, but it didn't work: mylist <- paste0(

Apply an already defined function to all dataframes at once

末鹿安然 提交于 2020-02-22 08:08:16
问题 I already have defined a function (which works fine). Nevertheless, I have 20 dataframes in the working space to which I want to lapply the same function (dat1 to dat20). So far it looks like this: dat1 <- func(dat=dat1) dat2 <- func(dat=dat2) dat3 <- func(dat=dat3) dat4 <- func(dat=dat4) ... dat20 <- func(dat=dat20) However, is there a way to do this more elegant with a shorter command, i.e. to lapply the function to all dataframes at once? I tried this, but it didn't work: mylist <- paste0(

Apply an already defined function to all dataframes at once

六月ゝ 毕业季﹏ 提交于 2020-02-22 08:07:14
问题 I already have defined a function (which works fine). Nevertheless, I have 20 dataframes in the working space to which I want to lapply the same function (dat1 to dat20). So far it looks like this: dat1 <- func(dat=dat1) dat2 <- func(dat=dat2) dat3 <- func(dat=dat3) dat4 <- func(dat=dat4) ... dat20 <- func(dat=dat20) However, is there a way to do this more elegant with a shorter command, i.e. to lapply the function to all dataframes at once? I tried this, but it didn't work: mylist <- paste0(

Find the names of constant columns in an R data.frame

时光毁灭记忆、已成空白 提交于 2020-02-05 06:19:19
问题 This is a follow-up on this question. In data.frame DATA , I have some columns that are constant numbers across the unique rows of the first column called study.name . For example, columns setting , prof and random are constant for all rows of Shin.Ellis and constant for all rows of Trus.Hsu and so on. Including Shin.Ellis and Trus.Hsu , there are 10 unique study.name rows. I wonder how to find the names of such constant columns? A solution was provided below (see NAMES ) but I wonder why

How do you write multiple rasters in [r]?

删除回忆录丶 提交于 2020-01-25 12:37:09
问题 I am using lapply() to load 144 rasters as in my previous post: How do you load multiple rasters in [r] using a for loop? library(raster) rastlist <- list.files(path=path, pattern='tif$', full.names=TRUE) allrasters <- lapply(rastlist, raster) allrasters ends up being a large list with 144 elements, of which 'name' looks like one of the attributes, I pasted the last (144th) element output below. [[144]] class : RasterLayer dimensions : 405, 345, 139725 (nrow, ncol, ncell) resolution : 30, 30

How do you write multiple rasters in [r]?

跟風遠走 提交于 2020-01-25 12:35:32
问题 I am using lapply() to load 144 rasters as in my previous post: How do you load multiple rasters in [r] using a for loop? library(raster) rastlist <- list.files(path=path, pattern='tif$', full.names=TRUE) allrasters <- lapply(rastlist, raster) allrasters ends up being a large list with 144 elements, of which 'name' looks like one of the attributes, I pasted the last (144th) element output below. [[144]] class : RasterLayer dimensions : 405, 345, 139725 (nrow, ncol, ncell) resolution : 30, 30

How do you write multiple rasters in [r]?

倖福魔咒の 提交于 2020-01-25 12:35:28
问题 I am using lapply() to load 144 rasters as in my previous post: How do you load multiple rasters in [r] using a for loop? library(raster) rastlist <- list.files(path=path, pattern='tif$', full.names=TRUE) allrasters <- lapply(rastlist, raster) allrasters ends up being a large list with 144 elements, of which 'name' looks like one of the attributes, I pasted the last (144th) element output below. [[144]] class : RasterLayer dimensions : 405, 345, 139725 (nrow, ncol, ncell) resolution : 30, 30

How to replace outliers with NA having a particular range of values in R?

亡梦爱人 提交于 2020-01-24 21:51:08
问题 I have climate data and I'm trying to replace outliers with NA . I'm not using boxplot(x)$out is because I have a range of values to be considered to compute the outlier. temp_range <- c(-15, 45) wind_range <- c(0, 15) humidity_range <- c(0, 100) My dataframe looks like this df with outliers (I highlighted values that should be replaced with NA according to ranges.) So temp1 and temp2 outliers must be replaced to NA according to temp_range , wind 's outliers should be replaced to NA according