r

Rolling window slider::slide() with grouped data

风格不统一 提交于 2021-02-11 07:44:12
问题 In the following example I try to compute the first coefficient from a linear model for time t = 1 until t. It's an expanding rolling window. It works well with ungrouped data, but when grouped by case, I get the error Error: Column coef1 must be length 10 (the group size) or one, not 30 . How can I handle grouped data? library(dplyr) library(slider) get_coef1 <- function(data) { coef1 <- lm(data = data, r1 ~ r2 + r3) %>% coef() %>% .["r2"] %>% unname() return(coef1) } data <- tibble(t = rep

Rolling window slider::slide() with grouped data

青春壹個敷衍的年華 提交于 2021-02-11 07:43:48
问题 In the following example I try to compute the first coefficient from a linear model for time t = 1 until t. It's an expanding rolling window. It works well with ungrouped data, but when grouped by case, I get the error Error: Column coef1 must be length 10 (the group size) or one, not 30 . How can I handle grouped data? library(dplyr) library(slider) get_coef1 <- function(data) { coef1 <- lm(data = data, r1 ~ r2 + r3) %>% coef() %>% .["r2"] %>% unname() return(coef1) } data <- tibble(t = rep

Filtering observations by using grep the reverse way in R

梦想与她 提交于 2021-02-11 07:43:10
问题 Shown as below: df <- data.frame(X1 = rep(letters[1:3],3), X2 = 1:9, X3 = sample(1:50,9)) df ind<- grep("a|c", df$X1) library(data.table) df_ac <- df[ind,] df_b <- df[!ind,] df_ac is created using the regular grep command. If I want to use the grep the reverse way: to select all observations with X1 == 'b' . I know I can do this by: ind2<- grep("a|c", df$X1, invert = T) df_b <-df[ind2,] But, in my original script, why does the command df_b <-df[!ind,] return a data frame with zero observation

Filtering observations by using grep the reverse way in R

五迷三道 提交于 2021-02-11 07:43:06
问题 Shown as below: df <- data.frame(X1 = rep(letters[1:3],3), X2 = 1:9, X3 = sample(1:50,9)) df ind<- grep("a|c", df$X1) library(data.table) df_ac <- df[ind,] df_b <- df[!ind,] df_ac is created using the regular grep command. If I want to use the grep the reverse way: to select all observations with X1 == 'b' . I know I can do this by: ind2<- grep("a|c", df$X1, invert = T) df_b <-df[ind2,] But, in my original script, why does the command df_b <-df[!ind,] return a data frame with zero observation

In order : launch new terminal, launch R environment in the new terminal, execute commands in R environment

被刻印的时光 ゝ 提交于 2021-02-11 07:34:57
问题 I would like a shell script that launches a new terminal, opens the R environment in the terminal and executes commands in the R environment of the new terminal. Here's what I did which is not working : #!/bin/sh for i in $(seq 25) do gnome-terminal -x sh "R; source('source.r'); function($i)" done Where, function() is an r function in the file "source.r" Please help. N.B. I don't want to launch the program using the command "Rscript" EDIT 1 : I don't want to use the command Rscript because

R - Files not found when call to import folder, but open as individual files and list.files shows all?

旧巷老猫 提交于 2021-02-11 07:32:42
问题 I have a bit of code used successfully before. It should import all files in a given directory into a single dataset. I have a new folder of data, and now I can't get it to work. The individual files will read in. List.files will also show all files in the folder. So I'm at a loss. list.files('../data/') [1] "B101-2.txt" "B101.txt" "B116.txt" "B6.txt" "B65.txt" "B67-2.txt" "B67.txt" [8] "B70.txt" "B71-2.txt" "B71.txt" "B95-2.txt" "B95.txt" "B96-2.txt" "B96.txt" [15] "B98-2.txt" "B98.txt" "B99

In order : launch new terminal, launch R environment in the new terminal, execute commands in R environment

亡梦爱人 提交于 2021-02-11 07:32:42
问题 I would like a shell script that launches a new terminal, opens the R environment in the terminal and executes commands in the R environment of the new terminal. Here's what I did which is not working : #!/bin/sh for i in $(seq 25) do gnome-terminal -x sh "R; source('source.r'); function($i)" done Where, function() is an r function in the file "source.r" Please help. N.B. I don't want to launch the program using the command "Rscript" EDIT 1 : I don't want to use the command Rscript because

Get min max of sliderInput in shiny

浪尽此生 提交于 2021-02-11 07:32:35
问题 I have a sliderInput element in my ui.R for which I determine the min and the max value dynamically (actually I use a module, where the values are determined from a data set). What I would like to do now is to read the min/max values in my server.R . Of course I could use the same code and determine min/max again, but I was wondering whether it is not possible to get the min/max values from the element directly? 回答1: As a workaround, you could set up reactives to calculate min and max , and

R - Files not found when call to import folder, but open as individual files and list.files shows all?

纵然是瞬间 提交于 2021-02-11 07:30:15
问题 I have a bit of code used successfully before. It should import all files in a given directory into a single dataset. I have a new folder of data, and now I can't get it to work. The individual files will read in. List.files will also show all files in the folder. So I'm at a loss. list.files('../data/') [1] "B101-2.txt" "B101.txt" "B116.txt" "B6.txt" "B65.txt" "B67-2.txt" "B67.txt" [8] "B70.txt" "B71-2.txt" "B71.txt" "B95-2.txt" "B95.txt" "B96-2.txt" "B96.txt" [15] "B98-2.txt" "B98.txt" "B99

I have suppressed warnings in knitr output but the warnings do not show up in the Rmarkdown console as expected. How do I view these?

左心房为你撑大大i 提交于 2021-02-11 07:26:51
问题 I am using Knitr and rmarkdown. I have suppressed warnings in the .pdf output and then typically the warnings are listed in the rmarkdown console. However, in the case of one specific report, instead of getting the warnings listed in the rmarkdown console, I get this message: There were 15 warnings (use warnings() to see them). Where do I write this warnings() code to see the list of warnings? I have tried adding warnings() at the bottom of my rmarkdown document but the output is: NULL. 回答1: