r

Generating strings combining letter and number sequences in R

北慕城南 提交于 2021-02-16 21:39:29
问题 How can i generate this vector in R: x <- c("R11", "R12", "R13", "R21", "R22", "R23", "R31",....) until R7xx for a set of six letters (R, S, D, A, B, X) ? and so on? Without actually typing it. 回答1: Try something like this: m <- expand.grid(c('A','B','D','R','X'),1:7,1:3) apply(m,1,paste0,collapse = "") One way to force a specific, non-alphabetic ordering would be to explicitly indicate it using a factor and then order the results from expand.grid : m <- expand.grid(factor(c('R','A','B','D',

How to reshape Panel / Longitudinal survey data from wide to long format using pivot_longer

安稳与你 提交于 2021-02-16 21:38:09
问题 I am attempting to use R to reshape a panel/longitudinal dataset entered from a survey from wide to long format. This is similar, but different from a previous question I asked Wide to long format with several variables Here is the basic data format: set.seed(1001) df <- data.frame( id = 1:2, Q1_Choice = sample(1:3,2), Q2_Choice = sample(1:3,2), Q3_Choice = sample(1:3,2), Q1_Status_Opt_1 = c(0,0), Q1_Status_Opt_2 = sample(1:40, 2), Q1_Status_Opt_3 = sample(1:40, 2), Q2_Status_Opt_1 = c(0,0),

Combine data frames from a vector of names

独自空忆成欢 提交于 2021-02-16 21:27:18
问题 I have an issue that I thought easy to solve, but I did not manage to find a solution. I have a large number of data frames that I want to bind by rows. To avoid listing the names of all data frames, I used "paste0" to quickly create a vector of names of the data frames. The problem is that I do not manage to make the rbind function identify the data frames from this vector of name. More explicitely: df1 <- data.frame(x1 = sample(1:5,5), x2 = sample(1:5,5)) df2 <- data.frame(x1 = sample(1:5,5

Read local csv in plumber API

狂风中的少年 提交于 2021-02-16 21:06:36
问题 I want to read and process a csv file by R using plumber. So far I found an ongoing discussion regarding file uploads in plumber on GitHub (though regarding binary files) and this SO answer (transforming JSON file), both supposing the use of postBody . I therefore started with this endpoint: library(plumber) #* parse csv file #* @param req the request object #* @post /file function(req) { result <- req$postBody return(result) }) When testing the endpoint using httr , I can read the file as

Creating a progress update by replacing output in the R console from C/C++

六月ゝ 毕业季﹏ 提交于 2021-02-16 21:05:03
问题 Is it possible to overwrite R console output with the C/C++ print functions for R? Rcpp::sourceCpp( code = ' #include <Rcpp.h> // [[Rcpp::export]] void print_test() { for(int i = 0; i < 10; i++) { std::stringstream strs; strs << "number: " << i; std::string temp_str = strs.str(); char const* char_type = temp_str.c_str(); REprintf(char_type); } }' ) print_test() The output of this function is number: 0number: 1number: 2number: 3number: 4number: 5number: 6number: 7number: 8number: 9 but I want

Read local csv in plumber API

时光毁灭记忆、已成空白 提交于 2021-02-16 21:02:56
问题 I want to read and process a csv file by R using plumber. So far I found an ongoing discussion regarding file uploads in plumber on GitHub (though regarding binary files) and this SO answer (transforming JSON file), both supposing the use of postBody . I therefore started with this endpoint: library(plumber) #* parse csv file #* @param req the request object #* @post /file function(req) { result <- req$postBody return(result) }) When testing the endpoint using httr , I can read the file as

Extracting Information from the Decision Rules in rpart package

社会主义新天地 提交于 2021-02-16 20:58:13
问题 I need to extract information from the rules in decision tree. I am using rpart package in R. I am using demo data in the package to explain my requirements: data(stagec) fit<- rpart(formula = pgstat ~ age + eet + g2 + grade + gleason + ploidy, data = stagec, method = "class", control=rpart.control(cp=0.05)) fit printing fit shows n= 146 node), split, n, loss, yval, (yprob) * denotes terminal node 1) root 146 54 0 (0.6301370 0.3698630) 2) grade< 2.5 61 9 0 (0.8524590 0.1475410) * 3) grade>=2

Extracting Information from the Decision Rules in rpart package

送分小仙女□ 提交于 2021-02-16 20:58:06
问题 I need to extract information from the rules in decision tree. I am using rpart package in R. I am using demo data in the package to explain my requirements: data(stagec) fit<- rpart(formula = pgstat ~ age + eet + g2 + grade + gleason + ploidy, data = stagec, method = "class", control=rpart.control(cp=0.05)) fit printing fit shows n= 146 node), split, n, loss, yval, (yprob) * denotes terminal node 1) root 146 54 0 (0.6301370 0.3698630) 2) grade< 2.5 61 9 0 (0.8524590 0.1475410) * 3) grade>=2

Unnesting a data frame containing lists

蹲街弑〆低调 提交于 2021-02-16 20:33:52
问题 I have a data frame that contains lists, like below: # Load packages library(dplyr) # Create data frame df <- structure(list(ID = 1:3, A = structure(list(c(9, 8), c(7,6), c(6, 9)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), B = structure(list(c(3, 5), c(2, 6), c(1, 5)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), C = structure(list(c(6, 5), c(7, 6), c(8, 7)), ptype = numeric(0), class = c("vctrs_list_of", "vctrs_vctr")), D = structure(list(c(5, 3), c(4,

R, data.table, group by column *numbers* AND sum a column

不打扰是莪最后的温柔 提交于 2021-02-16 20:24:08
问题 Let's say I have the following data.table > DT # A B C D E N # 1: J t X D N 0.07898388 # 2: U z U L A 0.46906049 # 3: H a Z F S 0.50826435 # --- # 9998: X b R L X 0.49879990 # 9999: Z r U J J 0.63233668 # 10000: C b M K U 0.47796539 Now I need to group by a pair of columns and calculate sum N. That's easy to do when you know column names in advance: > DT[, sum(N), by=.(A,B)] # A B V1 # 1: J t 6.556897 # 2: U z 9.060844 # 3: H a 4.293426 # --- # 674: V z 11.439100 # 675: M x 1.736050 # 676: U