lapply

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

只谈情不闲聊 提交于 2020-06-25 09:07:20
问题 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

Using lapply with if to test each element in a list

大兔子大兔子 提交于 2020-06-09 12:56:09
问题 Suppose I have a list: alist<- list(4,6,8,9) I want test if each list element is greater than 7 and return a list of 1 if its true and 0 if false. However I must use lapply. lapply(alist,if,>7,1) or lapply(alist,if,cond>7,1) Of course none of these work and I keep getting the following error. Error: unexpected ',' in "lapply(alist, if," 回答1: It pains me to answer this because it's very un R to do this. You could try being more explicit and use brackets as in: lapply(alist, function(x) if (x >

Splitting data into chunks and iterating over each chunk in R

霸气de小男生 提交于 2020-05-17 14:42:58
问题 I have a dataframe structured like this: birthwt tobacco01 pscore pscoreblocks blocknumber 3425 0 0.18 (0.177, 0.187] 1 3527 1 0.15 (0.158, 0.168] 2 1638 1 0.34 (0.335, 0.345] 3 Explaining the data : The birthwt column is a continuous variable measuring birth weight in grams. The tobacco01 column contains values of 0 or 1. The pscore column contains probability values between 0 and 1. The pscoreblocks takes the pscore column and breaks it down into 100 equally sized blocks. The block number

Splitting data into chunks and iterating over each chunk in R

一笑奈何 提交于 2020-05-17 14:42:33
问题 I have a dataframe structured like this: birthwt tobacco01 pscore pscoreblocks blocknumber 3425 0 0.18 (0.177, 0.187] 1 3527 1 0.15 (0.158, 0.168] 2 1638 1 0.34 (0.335, 0.345] 3 Explaining the data : The birthwt column is a continuous variable measuring birth weight in grams. The tobacco01 column contains values of 0 or 1. The pscore column contains probability values between 0 and 1. The pscoreblocks takes the pscore column and breaks it down into 100 equally sized blocks. The block number

nested sapply in R - breakdown

旧巷老猫 提交于 2020-04-18 05:44:18
问题 This post is related to my previous question about extracting data from nested lists, which has been answered. One of the answers contains a sapply function: usageExist <- sapply(garden$fruit, function(f){ sapply(garden$usage, '%in%', x = names(productFruit$type[[f]][["usage"]]))}) I am very new to data.table and apply functions and struggle to understand: what is happening in this particular line of code ? Why does cooking appear twice in the lists after running usageExists ? What is the

nested sapply in R - breakdown

丶灬走出姿态 提交于 2020-04-18 05:43:17
问题 This post is related to my previous question about extracting data from nested lists, which has been answered. One of the answers contains a sapply function: usageExist <- sapply(garden$fruit, function(f){ sapply(garden$usage, '%in%', x = names(productFruit$type[[f]][["usage"]]))}) I am very new to data.table and apply functions and struggle to understand: what is happening in this particular line of code ? Why does cooking appear twice in the lists after running usageExists ? What is the

How can i get the shapiro-wilk test

冷暖自知 提交于 2020-04-18 01:14:06
问题 I want to do the shapiro-wilk test for my regressions. Here is my code library(data.table) Regresiones_shapirotest_1<- data.table(tabla.Enero)[, .(Lm = lapply(.SD, function(x) resid(lm(tabla.Enero$PPNA ~ x)))), .SDcols = 15:93] I can get the test values only in the console Regresiones_shapirotest_1 [, lapply(Lm, shapiro.test)] [2] write.xlsx (Regresiones_shapirotest_1 , file = "shapirotest.xlsx", sheetName = "Hoja1", Col.names = TRUE, row.names = TRUE, append = FALSE) But when I try to export

How can i get the shapiro-wilk test

狂风中的少年 提交于 2020-04-18 01:11:32
问题 I want to do the shapiro-wilk test for my regressions. Here is my code library(data.table) Regresiones_shapirotest_1<- data.table(tabla.Enero)[, .(Lm = lapply(.SD, function(x) resid(lm(tabla.Enero$PPNA ~ x)))), .SDcols = 15:93] I can get the test values only in the console Regresiones_shapirotest_1 [, lapply(Lm, shapiro.test)] [2] write.xlsx (Regresiones_shapirotest_1 , file = "shapirotest.xlsx", sheetName = "Hoja1", Col.names = TRUE, row.names = TRUE, append = FALSE) But when I try to export

lapply function /loops on list of lists R

泄露秘密 提交于 2020-04-07 11:00:30
问题 I know this topic appeared on SO a few times, but the examples were often more complicated and I would like to have an answer (or set of possible solutions) to this simple situation. I am still wrapping my head around R and programming in general. So here I want to use lapply function or a simple loop to data list which is a list of three lists of vectors. data1 <- list(rnorm(100),rnorm(100),rnorm(100)) data2 <- list(rnorm(100),rnorm(100),rnorm(100)) data3 <- list(rnorm(100),rnorm(100),rnorm

How can I do 3064 regressions using the lapply function

岁酱吖の 提交于 2020-03-25 05:53:30
问题 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