[Solved] Crosschecking Two Text Files with R

So is it a text file or excel file you are importing? Without more details, file structure, or a reproducible example, it will be hard to help. You can try: # Get the correct package install.packages(‘readxl’) df1 <- readxl::read_excel(‘[directory name][file name].xlsx’) df2 <- readxl::read_excel(‘[directory name][file name].xlsx’) # Creates a new variable flag if miRNA from … Read more

[Solved] Using sub to reduce length of text [closed]

sub(“.*;\\s*”, “”, String) Explanation: .* – matches any number of characters at the beginning ; – matches the last ; \s* – matches zero or more white-space characters after the ; So the first expression matches everything up to the first non-blank character after the last ;. It is replaced with the empty string, so … Read more

[Solved] Frequency table with common values of 5 tables

Here’s my solution using purrr & dplyr: library(purrr) library(dplyr) lst1 <- list(mtcars=mtcars, iris=iris, chick=chickwts, cars=cars, airqual=airquality) lst1 %>% map_dfr(select, value=1, .id=”df”) %>% # select first column of every dataframe and name it “value” group_by(value) %>% summarise(freq=n(), # frequency over all dataframes n_df=n_distinct(df), # number of dataframes this value ocurrs dfs = paste(unique(df), collapse=”,”)) %>% filter(n_df … Read more

[Solved] Using R, how to reference variable variables (or variables variable) a la PHP

Consider using get() to obtain environment variables from string values. Additionally, consider the nested lapply() between dataframe and model lists for more organized returned object. Nested for loops would require appending each iteration into growing list unless you just need to output. Below examples use linear models, lm(): model1 <- y ~ x1 model2 <- … Read more

[Solved] How to add zoom option for wordcloud in Shiny (with reproducible example)

Normalisation is required only if the predictors are not meant to be comparable on the original scaling. There’s no rule that says you must normalize. PCA is a statistical method that gives you a new linear transformation. By itself, it loses nothing. All it does is to give you new principal components. You lose information … Read more

[Solved] Plot with confidence intervals from 1D data?

As the first google match stated, you can try doing as: n<-50 x<-sample(40:70,n,rep=T) y<-.7*x+rnorm(n,sd=5) plot(x,y,xlim=c(20,90),ylim=c(0,80)) mylm<-lm(y~x) abline(mylm,col=”red”) newx<-seq(20,90) prd<-predict(mylm,newdata=data.frame(x=newx),interval = c(“confidence”), level = 0.90,type=”response”) lines(newx,prd[,2],col=”red”,lty=2) lines(newx,prd[,3],col=”red”,lty=2) Of course it is one of possibilities Another example would be: x <- rnorm(15) y <- x + rnorm(15) new <- data.frame(x = seq(-3, 3, 0.5)) pred.w.clim <- predict(lm(y … Read more

[Solved] Matlab/Excel/R: Transforming an unbalanced dataset depending on value of column [closed]

A solution in Matlab: A = [1 5; 1 5; 1 6; 2 4; 2 2; 3 8; 3 4]; nMaxDays = max(A(:, 1)); nMaxSamples = max(accumarray(A(:, 1), 1)); mnSamplesMatrix = nan(nMaxSamples, nMaxDays); for (nDay = unique(A(:, 1))’) vnThisDay = A(A(:, 1) == nDay, 2); mnSamplesMatrix(1:numel(vnThisDay), nDay) = vnThisDay; end 0 solved Matlab/Excel/R: Transforming an … Read more

[Solved] How to pivot my data frame in R so that it turns into another data frame in this specific format? [duplicate]

library(tidyr) df %>% gather(value=”amount”,key=’Type’,-SubDept2) %>% head(n=5) SubDept2 Type amount 1 Admin BasicSalary 10000 2 Bar BasicSalary 9880 3 Entertainment BasicSalary 11960 4 F&B BasicSalary 9680 5 Finance BasicSalary 10310 2 solved How to pivot my data frame in R so that it turns into another data frame in this specific format? [duplicate]

[Solved] Pass sequentially named variables to functions within a loop in R

Solved it with eval and parse: for(i in 1:length(vec)){ assign(paste(“holder”,i,sep=””),vec[i]) print ( paste (“holder”,i,sep=””) ) positions<-c(positions,grep( eval( parse(text= paste (“holder”,i,sep=””)) ),colnames(data),ignore.case=TRUE)) } solved Pass sequentially named variables to functions within a loop in R

[Solved] Extract date and time from datetime field in R

If I understood well, R can read correctly your dates and times as you import your data (because they are in POSIXct format), but you can not extract the date and the time in the right format from your date-time column. Considering that you have a data.frame in R, like this: date_time Sold 1 2020-01-01 … Read more

[Solved] How to identify the virality growth rate in time series data using R? [closed]

Although this question is very likely to get closed in the coming hours if you dont make your problem clearer, this might get you started at least: mydf <- scan(textConnection(“12376 167827 3454596 9676112 342102 1232103 546102 5645696 96767110 23423119 4577140 45435158 56767138 635435167 35443160 34534166 3213133 2132148 2342130 7656127 43234117 56545130 5645138 56455149”), ) plot(mydf, … Read more

[Solved] Merge rows with same names and sum other values in other column rows [duplicate]

We could first group_by country and then use summarise with across library(dplyr) df %>% group_by(country) %>% summarise(across(everything(), sum)) Output: country new_persons_vac~ total_persons_v~ new_persons_ful~ total_persons_f~ new_vaccine_dos~ total_vaccine_d~ <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 Afghan~ 294056 8452317 163535 2313338 457591 10765655 2 Albania 601152 27639676 465433 18105836 459226 45745512 3 Andorra 40569 360995 25838 144358 … Read more

[Solved] copy rasters within stack

Something like this? # example data r <- raster(ncol=10, nrow=10) r[]=1:ncell(r) x <- brick(r,r,r,r,r,r) x <- x * 1:6 y <- list() for (i in 1:nlayers(x)) { r <- raster(x, i) y <- c(y, r, r, r) } s <- stack(y) solved copy rasters within stack