lapply

R - How to vectorize with apply family function and avoid while/for loops in this case?

岁酱吖の 提交于 2019-12-22 00:20:27
问题 In this case (more details could be found in this question: Count how many observations in the rest of the dat fits multiple conditions? (R)) This is a dataset called event, containing thousands of events (observations) and I selected several rows to show you the data structure. It contains the "STATEid", "date" of occurrence, and geographical coordinates in two variables "LON" "LAT". I am writing to calculate a new variable (column) for each row. This new variable should be: "Given any

Aggregate sum obs with different ID's in the same data frame

拟墨画扇 提交于 2019-12-20 06:14:24
问题 My goal is to make another column by summing the observation from the present day and all previous observations from the same ID by using the date (the data set is sorted in date and chr nr(ID). I will need the aggregation to start over when a new "id" is presented. there might be som NA's, they should be considered as null "Doseringer_pr_kg_dyr" is the observation. CHR_NR DATO_AFSLUT Doseringer_pr_kg_dyr brugstid 10358 2018-08-06 29416.67 31 10358 2018-09-06 104682.27 36 10358 2018-10-12

Joining the result of two statistical tables in one table in R

北战南征 提交于 2019-12-20 04:54:06
问题 In continuation of this issue comparison Mann-Whitney test between groups, I decided to create a new topic. Solution of Rui Barradas helped me calculate Mann-Whitney for group 1-2 and 1-3. lst <- split(mydat, mydat$group) lapply(lst[-1], function(DF) wilcox.test(DF$var, lst[[1]]$var, exact = FALSE)) So now i want get the descriptive statistics. I use library:psych describeBy(mydat$var,mydat$group) So i get the following output group: 1 vars n mean sd median trimmed mad min max range skew

display predicted values for initial data using auto.arima in R

家住魔仙堡 提交于 2019-12-20 04:53:04
问题 Let's work with this data sample timeseries<-structure(list(Data = structure(c(10L, 14L, 18L, 22L, 26L, 29L, 32L, 35L, 38L, 1L, 4L, 7L, 11L, 15L, 19L, 23L, 27L, 30L, 33L, 36L, 39L, 2L, 5L, 8L, 12L, 16L, 20L, 24L, 28L, 31L, 34L, 37L, 40L, 3L, 6L, 9L, 13L, 17L, 21L, 25L), .Label = c("01.01.2018", "01.01.2019", "01.01.2020", "01.02.2018", "01.02.2019", "01.02.2020", "01.03.2018", "01.03.2019", "01.03.2020", "01.04.2017", "01.04.2018", "01.04.2019", "01.04.2020", "01.05.2017", "01.05.2018", "01

using lapply function and list in r

若如初见. 提交于 2019-12-20 04:22:07
问题 d1 <- data.frame(col_one = c(1,2,3),col_two = c(4, 5, 6)) d2 <- data.frame(col_one = c(1, 1, 1), col_two = c(6, 5, 4)) d3 <- data.frame(col_one = c(7, 1, 1), col_two = c(8, 5, 4)) my.list <- list(d1, d2,d3) for (i in 1:3) { table<- lapply(my.list, function(data, count) { sql <- #sqldf( paste0( "select *,count(col_one) from data where col_one = ", count," group by col_one" ) #) print(sql) }, count = i) } output: [1] "select *,count(col_one) from data where col_one = 1 group by col_one" [1]

Processing the list of data.frames with “apply” family of functions

爷,独闯天下 提交于 2019-12-20 04:16:02
问题 I have a data frame which I then split into three (or any number) of dataframes. What I’m trying to do is to automatically process each column in each dataframe and add lagged versions of existing variables. For example if there were three variables in each data.frame (V1, V2, V3) I would like to automatically (without hardcoding) add V1.lag, V2.lag and V3.lag. Here is what I have so far, but I’m stuck now. Any help would be highly apprecaited. dd<-data.frame(matrix(rnorm(216),72,3),c(rep("A"

How to use lapply and paste on multiple dataframes in a list

给你一囗甜甜゛ 提交于 2019-12-20 02:59:14
问题 I can't combine the use of lapply and paste to combine two columns for multiple dataframes contained within a list. I've looked through multiple sources, but can't find the answer. This answer Apply paste over a list of vectors to get a list of strings is about combining rows within lists, not combining columns to obtain a vector. This answer explains how to select columns but not paste them together using lapply on a list of dataframes This page explains how to access columns in lists, but

Find variables that occur only in one cluster in data.frame in R

亡梦爱人 提交于 2019-12-19 10:47:29
问题 Using BASE R, I wonder how to answer the following question: Are there any value on X or Y (i.e., variables of interest names) that occurs only in one element in m (as a cluster) but not others? If yes, produce my desired output below. For example: Here we see X == 3 only occurs in element m[[3]] but not m[[1]] and m[[2]] . Here we also see Y == 99 only occur in m[[1]] but not others. Note: the following is a toy example, a functional answer is appreciated. AND X & Y may or may not be numeric

Apply common function to all data frames and return data frames with same name

巧了我就是萌 提交于 2019-12-19 10:07:04
问题 I'm trying to apply a function to all similarly spelled data frames in my global environment in R. I want to apply this function to all these data frames, but I can't figure out how to do it without me specifying 1 by 1. I want to return the data frame to the global environment with the same spelling as it was before. mtcars_test = mtcars iris_test = iris #....etc......could be 2 of them or 88 of them...but they will all end in "_test" # figure out what data frames I am working with list_of

How to define multiple variables with lapply?

邮差的信 提交于 2019-12-19 07:13:07
问题 I want to apply a function with multiple variables with different values to a list. I know how to do this with one changing variable sapply(c(1:10), function(x) x * 2) # [1] 2 4 6 8 10 12 14 16 18 20 but not with two. I show you first manually what I want (actually I use lapply() but sapply() is more synoptic in SO): # manual a <- sapply(c(1:10), function(x, y=2) x * y) b <- sapply(c(1:10), function(x, y=3) x * y) c <- sapply(c(1:10), function(x, y=4) x * y) c(a, b, c) # [1] 2 4 6 8 10 12 14