[Solved] Row-wise sum for select columns while ignoring NAs [closed]

Here is one way to accomplish this: #make a data frame df <- data.frame(a = c(1,2,3), b = c(1,NA,3), c = c(1,2,3)) #use rowSums on a dataframe made with each of your columns and specify na.rm = TRUE df$mySum <- rowSums(cbind(df$a,df$b), na.rm = TRUE) 0 solved Row-wise sum for select columns while ignoring NAs [closed]

[Solved] Extract and paste together multiple columns of a data frame like object using a vector of column names

We may use either of the following: do.call(function (…) paste(…, sep = “-“), rld[groups]) do.call(paste, c(rld[groups], sep = “-“)) We can consider a small, reproducible example: rld <- mtcars[1:5, ] groups <- names(mtcars)[c(1,3,5,6,8)] do.call(paste, c(rld[groups], sep = “-“)) #[1] “21-160-3.9-2.62-0” “21-160-3.9-2.875-0” “22.8-108-3.85-2.32-1” #[4] “21.4-258-3.08-3.215-1” “18.7-360-3.15-3.44-0” Note, it is your responsibility to ensure all(groups %in% names(rld)) … Read more

[Solved] In ggplot, how can I plot multiple graphs in the same window? [closed]

The following should work, I have stored your example input as a dataframe called ‘dat’: library(ggplot2) library(cowplot) plt = ggplot(dat, aes(Income_group, percentage)) + geom_bar(stat=”identity”) + facet_grid(Airport ~.) + background_grid(major=”y”, minor = “none”) + panel_border() plt + theme(axis.text.x = element_text(angle = 90, hjust = 1)) 2 solved In ggplot, how can I plot multiple graphs in … Read more

[Solved] Loop R with Quandl as optional

This answer assumes reading of the earlier question and comments so is not “code only”. qt = expand.grid(Month=c(“G”,”J”,”M”,”Q”,”V”), Year=1975:2016) query_names_vec <- apply(qt, 1, function(x) paste0(“CME/GC”, paste0( x, collapse=””) ) ) > head( query_names_vec ) [1] “CME/GCG1975” “CME/GCJ1975” “CME/GCM1975” “CME/GCQ1975” [5] “CME/GCV1975” “CME/GCG1976” 10 solved Loop R with Quandl as optional

[Solved] Function in R that returns first word in a sentence that is having a length which is an even number & also longest even word

You can do this using the popular stringr and dplyr libraries. library(dplyr) library(stringr) df <- tibble( sentence = c( “Time & tide waits for none”, ” Tit for tat”, “Eyes are mirror of person’s thoughts”, “Some Other Sentence”, “Odd sentences failure” ) ) df <- df %>% # Split the sentence and store it in … Read more

[Solved] how to compare words and find match words in R

I don’t think data.frame() can handle vectors as individual elements, so I used data_frame() from the tibble package: df <- tibble::data_frame(var1 = list(c(“apple”, “www”), c(“dog”, “cat”, “kkk”)), var2 = list(c(“apple”, “zzz”), c(“cat”, “kkk”))) apply function by row, where the function takes the intersection of the first and second list elements: apply(df, 1, function(x) intersect(x[[1]], x[[2]])) … Read more

[Solved] plot multiple lines in ggplot

I’ll make some fake data (I won’t try to transcribe yours) first: set.seed(2) x <- data.frame( Date = rep(Sys.Date() + 0:1, each = 24), # Year, Month, Day … are not used here Hour = rep(0:23, times = 2), Value = sample(1e2, size = 48, replace = TRUE) ) This is a straight-forward ggplot2 plot: … Read more

[Solved] R function calculating statistic of variable column name [duplicate]

You seem to be asking how to use a column name passed as an argument to a function?? myfunction <- function(df, col) mean(df[,col], na.rm=T) # test set.seed(1) df <- data.frame(x=rnorm(10),y=rnorm(10)) myfunction(df,”x”) # [1] 0.1322028 This also works if you pass a column number. myfunction(df,1) # [1] 0.1322028 1 solved R function calculating statistic of variable … Read more

[Solved] Convert numbers to time in R

Converting a number of seconds (such as 75) to a decimal format such as minutes.seconds is a bit odd, but you could do it this way: a <- 75 hour <- floor(a / 60) # floor() rounds down to the last full hour minute <- a %% 60 * 0.01 # the %% operator gives … Read more

[Solved] R: Not meaningful for factors even though its data type is numeric [duplicate]

We can convert those factor columns to numeric library(dplyr) library(magrittr) mydata %<>% mutate_if(is.factor, funs(as.numeric(as.character(.)))) Or using base R i1 <- sapply(mydata, is.factor) mydata[i1] <- lapply(mydata[i1], function(x) as.numeric(as.character(x))) and then the code should work 2 solved R: Not meaningful for factors even though its data type is numeric [duplicate]