r

Vectorized implementation of exponentially weighted moving standard deviation using R?

纵然是瞬间 提交于 2021-02-20 05:13:26
问题 I am trying to implement a vectorized exponentially weighted moving standard deviation using R. Is this the correct approach? ewma <- function (x, alpha) { c(stats::filter(x * ratio, 1 - ratio, "recursive", init = x[1])) } ewmsd <- function(x, alpha) { sqerror <- na.omit((x - lag(ewma(x, ratio)))^2) ewmvar <- c(stats::filter(sqerror * ratio, 1 - ratio, "recursive", init = 0)) c(NA, sqrt(ewmvar)) } I'm guessing it's not, since its output is different from Python's pandas.Series.ewm.std()

Vectorized implementation of exponentially weighted moving standard deviation using R?

▼魔方 西西 提交于 2021-02-20 05:12:33
问题 I am trying to implement a vectorized exponentially weighted moving standard deviation using R. Is this the correct approach? ewma <- function (x, alpha) { c(stats::filter(x * ratio, 1 - ratio, "recursive", init = x[1])) } ewmsd <- function(x, alpha) { sqerror <- na.omit((x - lag(ewma(x, ratio)))^2) ewmvar <- c(stats::filter(sqerror * ratio, 1 - ratio, "recursive", init = 0)) c(NA, sqrt(ewmvar)) } I'm guessing it's not, since its output is different from Python's pandas.Series.ewm.std()

Calculate Run Length Sequence and Maximum by Subject ID

喜你入骨 提交于 2021-02-20 04:36:12
问题 We have time series data in which repeated observations were measured for several subjects. I would like to calculate the number of occasions in which the variable positive == 1 occurs for each subject (variable id ). A second aim is to identify the maximum length of these runs of consecutive observations in which positive == 1 . For each subject there are likely to be multiple runs within the study period. Rather than calculating the maximum number of consecutive positive observations per

How to get HTML element that is before a certain class?

大憨熊 提交于 2021-02-20 04:30:28
问题 I'm scraping and having trouble getting the element of the “th” tag that comes before the other “th” element that contains the “type2” class. I prefer to take it by identifying that it is the element "th" before the "th" with class "type2" because my HTML has a lot of "th" and that was the only difference I found between the tables. Using rvest or xml2 (or other R package), can I get this parent? The content which I want is "text_that_I_want". Thank you! <tr> <th class="array">text_that_I

How to get HTML element that is before a certain class?

给你一囗甜甜゛ 提交于 2021-02-20 04:29:20
问题 I'm scraping and having trouble getting the element of the “th” tag that comes before the other “th” element that contains the “type2” class. I prefer to take it by identifying that it is the element "th" before the "th" with class "type2" because my HTML has a lot of "th" and that was the only difference I found between the tables. Using rvest or xml2 (or other R package), can I get this parent? The content which I want is "text_that_I_want". Thank you! <tr> <th class="array">text_that_I

lapply / purrr::map like function that allows access to the index by default?

帅比萌擦擦* 提交于 2021-02-20 04:29:08
问题 There's a workaround to allow access the index inside a s/lapply e.g. x <- list(a=11,b=12,c=13) lapply(seq_along(x), function(y, n, i) { paste(n[[i]], y[[i]]) }, y=x, n=names(x)) Is there any function like s/lapply (or like purrr::map() ) which allows access to the index in the simplest way possible, which I guess would be to simply supply its desired name to the initial function call and nothing more; map_with_index <- function(.x, .f, index) { # Same as purrr::map() # ..but whatever string

Go from long to wide using tidyr's pivot_wider

a 夏天 提交于 2021-02-20 04:26:28
问题 I have a simple long df where every element in the fi column should be a new column in the wide df. So the new df should have 10 columns A:J. The wide df should have two rows, "one" and "two". Sounds like a job for pivot_wider, but I couldn't get it to work. library("tidyverse") df <- structure(list(fi = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"), one = c(0.5, 1.4, 0.89, 1.4, 1.45, 1.25, 1.45, 1.4, 1.4, 1.5), two = c(0.75, 1.6, 1.05, 1.6, 1.45, 1.05, 1.65, 1.5, 1.55, 1.65)), row

Calculate mean of column data based on conditions in another column [duplicate]

强颜欢笑 提交于 2021-02-20 04:18:27
问题 This question already has answers here : R: Calculate means for subset of a group (3 answers) Closed 3 years ago . I have a dataframe with a transnational data structure that looks something similar to the below: ID RANK GRADE 123 E1 0 123 E1 42 123 E1 NA 123 E2 41 123 E2 42 456 E2 41 456 E2 41 456 E3 NA I want to calculate the mean of the Grade column for each Rank based on the ID, ignoring the values that are 0 because they are data entry errors, and ignoring the NA's. For example: For ID

how do you extract the columns that contain a certain text/string in R

点点圈 提交于 2021-02-20 04:17:25
问题 I need to be able to extract columns that contain exact string that I am looking for. For example, I have this data frame x: structure(list(Time = structure(1L, .Label = "1/1/2015", class = "factor"), WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.MB. = 3555L, WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Phys.Mem.Free.MB. = 55L, WTAD..Linux..Linux.Percent.of.Physical.Memory.and.Swap.Used.on.web02.Total.Swap.Free.MB. = 44L,

lapply aggregate columns in multiple dataframes R

走远了吗. 提交于 2021-02-20 04:16:32
问题 I have several dataframes in a list in R. There are entries in each of those DF I would like to summarise. Im trying to get into lapply so that would be my preferred way (though if theres a better solution I would be happy to know it and why). My Sample data: df1 <- data.frame(Count = c(1,2,3), ID = c("A","A","C")) df2 <- data.frame(Count = c(1,1,2), ID = c("C","B","C")) dfList <- list(df1,df2) > head(dfList) [[1]] Count ID 1 1 A 2 2 A 3 3 C [[2]] Count ID 1 1 C 2 1 B 3 2 C I tried to