r

Ranking multiple columns by different orders using data table

99封情书 提交于 2021-02-16 14:32:08
问题 Using my example below, how can I rank multiple columns using different orders, so for example rank y as descending and z as ascending? require(data.table) dt <- data.table(x = c(rep("a", 5), rep("b", 5)), y = abs(rnorm(10)) * 10, z = abs(rnorm(10)) * 10) cols <- c("y", "z") dt[, paste0("rank_", cols) := lapply(.SD, function(x) frankv(x, ties.method = "min")), .SDcols = cols, by = .(x)] 回答1: data.table 's frank() function has some useful features which aren't available in base R's rank()

Double left join in dplyr to recover values

微笑、不失礼 提交于 2021-02-16 14:29:40
问题 I've checked this issue but couldn't find a matching entry. Say you have 2 DFs: df1:mode df2:sex 1 1 2 2 3 And a DF3 where most of the combinations are not present, e.g. mode | sex | cases 1 1 9 1 1 2 2 2 7 3 1 2 1 2 5 and you want to summarise it with dplyr obtaining all combinations (with not existent ones=0): mode | sex | cases 1 1 11 1 2 5 2 1 0 2 2 7 3 1 2 3 2 0 If you do a single left_join (left_join(df1,df3) you recover the modes not in df3, but 'Sex' appears as 'NA', and the same if

How to avoid the crossing effect in legend with geom_vline() and geom_hline on the same scatter plot?

谁都会走 提交于 2021-02-16 14:26:52
问题 I created a scatter plot with geom_hline() and geom_vline() , the plot is good but the legend entries are not how I would like to make them appear. The vline (Restauration) and hline (Threshold) are crossing each other in the legend, making it confusing. I want the restauration legend entry to be an orange vertical line and the Threshold legend entry to be a horizontal black line. I tried several things suggested in other posts, with guide_legend(override.aes()) or with show.legend = F but

use ifelse to create new column in r

霸气de小男生 提交于 2021-02-16 14:24:08
问题 My code is: data$E<-ifelse(data$D==data$B, "b", ifelse(data$D==data$C, "c", "unknowwn")) result: A B C D E 1 16 16 NA 16 b 2 20 NA 20 20 NA 3 24 NA NA 24 NA But what I want is: A B C D E 1 16 16 NA 16 b 2 20 NA 20 20 c 3 24 NA NA 24 unknowwn Dose anyone know how to solve this problem? Thanks! 回答1: data$E <- ifelse(!is.na(data$B) & data$D == data$B, "b", ifelse(!is.na(data$C) & data$D == data$C, "c", "unknowwn")) 回答2: Your condition is a bit complicated as you need to consider if either of

How do I get mean functions to work when I use piping?

两盒软妹~` 提交于 2021-02-16 14:19:27
问题 This is probably a simple question, but I'm having trouble getting the mean function to work using dplyr. Using the mtcars dataset as an example, if I type: data(mtcars) mtcars %>% select (mpg) %>% mean() I get the "Warning message: In mean.default(.) : argument is not numeric or logical: returning NA" error message. For some reason though if I repeat the same code but just ask for a "summary", or "range" or several other statistical calculations, they work fine: data(mtcars) mtcars %>%

R filter rows based on multiple partial strings applied to multiple columns

女生的网名这么多〃 提交于 2021-02-16 14:11:15
问题 Sample of dataset: diag01 <- as.factor(c("S7211","J47","J47","K729","M2445","Z509","Z488","R13","L893","N318","L0311","S510","A047","D649")) diag02 <- as.factor(c("K590","D761","J961","T501","M8580","R268","T831","G8240","B9688","G550","E162","T8902","E86","I849")) diag03 <- as.factor(c("F058","M0820","E877","E86","G712","R32","A408","E888","G8220","C794","T68","L0310","M1094","D469")) diag04 <- as.factor(c("E86","C845","R790","I420","G4732","R600","L893","R509","T913","C795","M8412","G8212",

R remove words from sentences in dataframe

点点圈 提交于 2021-02-16 14:06:50
问题 I have one dataframe with two columns which each containing sentences and I would like to subtract one from the other. I somehow can't easily find a method to do the following: > c1 <- c("A short story","Not so short") > c2 <- c("A short", "Not so") > data.frame(c1, c2) which should give the result of c1 - c2 "story","short" Any ideas are helpful. 回答1: We can use str_remove which is vectorized library(stringr) library(dplyr) df1 %>% mutate(c3 = str_remove_all(c1, c2)) c1 c2 c3 #1 A short

R remove words from sentences in dataframe

我与影子孤独终老i 提交于 2021-02-16 14:06:09
问题 I have one dataframe with two columns which each containing sentences and I would like to subtract one from the other. I somehow can't easily find a method to do the following: > c1 <- c("A short story","Not so short") > c2 <- c("A short", "Not so") > data.frame(c1, c2) which should give the result of c1 - c2 "story","short" Any ideas are helpful. 回答1: We can use str_remove which is vectorized library(stringr) library(dplyr) df1 %>% mutate(c3 = str_remove_all(c1, c2)) c1 c2 c3 #1 A short

R trycatch() in place with err and warn handlers but Shiny still crashes?

给你一囗甜甜゛ 提交于 2021-02-16 13:53:22
问题 To reproduce: #app.R library(shiny) library(RODBC) savefunc <- function() { conn <- odbcConnect(...) #put in a conn string u know works df = data.frame(testing=c("testing")) columnTypes = list(testing="varchar(128)") tryCatch( { sqlSave(conn, dat=df, tablename ="...", #put in a pre-existing tbl rownames = FALSE, colnames = FALSE, append=TRUE, varTypes=columnTypes) }, err=function(errorCondition) { cat("in err handler") message(errorCondition) }, warn=function(warningCondition) { cat("in warn

Plotting a list of timeseries of class(forecast) in [R]

拥有回忆 提交于 2021-02-16 13:47:20
问题 I am trying to plot a faceted grid of timeseries plots (ideally 3X3) using a list of forecast timeseries data. The data is nested within a list and is of class forecast::forecast. > class(forecasts) [1] "list" > class(forecasts$`1_1`) [1] "forecast" > head(forecasts, 2) $`1_1` Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 Dec 2016 7.370299 7.335176 7.405422 7.316583 7.424015 $`1_10` Point Forecast Lo 80 Hi 80 Lo 95 Hi 95 Dec 2016 7.396656 7.359845 7.433467 7.340359 7.452953 I would like to plot the