[Solved] Shifting strings in R

Data library (dplyr) df <- data.frame( x = c(“head”, “hand”, “key”), y = c(“car”, “nose”, “house”), z = c(“toe”, “mouse”, “ball”), stringsAsFactors = FALSE ) Code # Add new column. df_result <- df %>% mutate( new = case_when( x %in% c(“head”, “hand”) ~ x, z == “ball” ~ z, TRUE ~ NA_character_ ) ) # … Read more

[Solved] Sum rows by interval Dataframe

If you are looking for an R solution, here’s one way to do it: The trick is using [ combined with rowSums FRAMETRUE$Group1 <- rowSums(FRAMETRUE[, 2:8], na.rm = TRUE) solved Sum rows by interval Dataframe

[Solved] vector addition using R

You can use ls() to list all variables in the global environment and restrict the output of it by pattern argument. Then get values of those variables using mget and row bind the values of the list using rbind and get row sum using rowSums function. # create sample vectors c1 <- c(1, 2) c2 … Read more

[Solved] Extract with Specific Prefix and control no. of digits in Regex- R

You could try the below code which uses word boundary \b. Word boundary is used to match between a word character and a non-word character. > library(stringr) > str_extract_all(x, perl(‘\\b(?:[35948]\\d{9}|TAM\\d{5}|E\\d{7}|A\\d{5})\\b’)) [[1]] [1] “3234567890” “5234567890” “9234567890” “4234567890” “8234567890” [6] “TAM12345” “E1234567” “A12345” solved Extract with Specific Prefix and control no. of digits in Regex- R

[Solved] Jumping to the next available date when merging panels

It’s more or less the same as your other question. The only change is that you’ll have to set “company,date” as the key columns to perform the join on (note that the order is important – it’ll first sort by company and then by date). require(data.table) ## 1.9.2 setDT(df) setDT(event) setkey(df, company1, date1) setkey(event, company2, … Read more

[Solved] Drop part of an integer in R [duplicate]

Your expected output seem to be a string, I would suggest you will stay in the integer world for efficiency and convenience, something like (the idea’s taken from here) ((dataset / 100) %% 1) * 100 ## [1] 1 2 3 4 5 6 7 8 9 10 11 12 ## OR just `dataset – … Read more

[Solved] Add regression line legend to geom_abline

With slight modification your code works just fine: ggplot() + geom_point(mapping = aes(x = X, y = y)) + geom_abline(aes(colour = “line_1”, intercept = -0.9930872, slope = 0.4866284)) + geom_abline(aes(colour = “line_2”, intercept = -1, slope = 0.5)) + scale_colour_manual(name = “lines”, values = c(“red”, “blue”)) + theme(legend.position = “bottom”) Added legend position in case … Read more

[Solved] How to reproduce a table of student? [closed]

Something like x <- 1:20; names(x) <-x y <- c(0.05, 0.025, 0.01, 0.005, 0.001, 0.0005); names(y) <- y outer(x, y,function(x, y) {abs(qt(y, df=x))}) #> 0.05 0.025 0.01 0.005 0.001 5e-04 #> 1 6.313752 12.706205 31.820516 63.656741 318.308839 636.619249 #> 2 2.919986 4.302653 6.964557 9.924843 22.327125 31.599055 #> 3 2.353363 3.182446 4.540703 5.840909 10.214532 12.923979 #> … Read more

[Solved] Selecting column sequences and creating variables

We could split the dataset (‘df’) with ‘1416’ columns to equal size ‘118’ columns by creating a grouping index with gl lst <- setNames(lapply(split(1:ncol(df), as.numeric(gl(ncol(df), 118, ncol(df)))), function(i) df[,i]), paste0(‘site’, 1:12)) Or you can create the ‘lst’ without using the split lst <- setNames(lapply(seq(1, ncol(df), by = 118), function(i) df[i:(i+117)]), paste0(‘site’, 1:12)) If we need … Read more

[Solved] R-project Graphic plot [closed]

I think the two outputs below are ~pub-ready. The first uses base R and jitter, used to add some noise to data so that points with the very same coordinates appear on different positions. That’s a nice approach in such case (providing you mention the jittering as data are slightly modified). If you have many … Read more

[Solved] r: convert a string to date [duplicate]

We can use sub to create a – between the first 4 characters and the next 2. Match the four characters (.{4}), place it in a capture groups ((…)), followed by the next 2 characters in another capture group, replace it with the backreference for those groups (\\1, \\2) and in between we add the … Read more

[Solved] how to avoid repeated codes in R

I think you may want either of these two new_table %>% group_by(sex, category, flag, day) %>% mutate(mean = mean(value), standardDeviation = sd(value)) or new_table %>% group_by(sex, category, flag, day) %>% summarise(mean = mean(value), standardDeviation = sd(value)) 1 solved how to avoid repeated codes in R