[Solved] Explanation for If statement in R

file$Efficiency <- cut(file$Duration, breaks=c(-1,5,15,30,200), labels=c(“Above Par”, “Par”, “Below Par”, “Unacceptable”)) Here is a little example of data from me: x <- c(0,1, 4:6, 14:16, 29:31) y <- cut(x, breaks=c(-1,5,15,30,200), labels=c(“Above Par”, “Par”, “Below Par”, “Unacceptable”)) data.frame(x=x, y=y) 1 solved Explanation for If statement in R

[Solved] R programming – if/else command [closed]

x <- c(.5, 0.2, 2, 3.4, 0.4, 1.2, .2, .8, 2.3, 7.4) result.final <- NULL cumul.total <- 0 for (i in x) { cumul.total <- cumul.total + i if(i > 1){ result <- cumul.total } else { next } result.final <- c(result.final, result) } result.final 8 solved R programming – if/else command [closed]

[Solved] Including different numbers in the names of dataframes

Although I agree with joran in this case, sometimes it can be useful to use the assign() function, for instance as follows: for (i in 1:3) { assign(paste0(“data.”, i), i) } This results in the following: > ls() [1] “data.1” “data.2” “data.3” “i” > data.1 [1] 1 > data.2 [1] 2 > data.3 [1] 3 … Read more

[Solved] Deleting specific rows with a pattern[start and end indicators] from a dataframe [closed]

Using a toy example… df <- data.frame(a=LETTERS[1:10],b=LETTERS[3:12],stringsAsFactors = FALSE) limits <- c(“E”,”H”) sapply(df,function(x){ del.min <- grep(limits[1],x) del.max <- grep(limits[2],x) x[del.min:del.max] <- “” return(x)}) a b [1,] “A” “C” [2,] “B” “D” [3,] “C” “” [4,] “D” “” [5,] “” “” [6,] “” “” [7,] “” “I” [8,] “” “J” [9,] “I” “K” [10,] “J” “L” … Read more

[Solved] Line chart with categorical values in ggplot2?

1) The issue is that sales_clean$Year is a factor. 2) ggplot interprit your x-value as categorical, y-value as continous and aggregated value into the bar plot (instead bar there are lines). Please see the simulation: library(ggplot2) set.seed(123) sales_clean <- data.frame(Year = rep(factor(2014:2018), 1000), Net_Rev = abs(rnorm(5000))) plotLine <- ggplot(sales_clean, aes(Year, Net_Rev, na.rm = FALSE)) plotLine … Read more

[Solved] Remove NA inside column from Datafarme [closed]

We loop through the columns, remove the NA elements, then select the minimum number of observations after comparing all the elements in the list. lst <- lapply(df1, function(x) x[complete.cases(x)]) res <- data.frame(lapply(lst, `length<-`,min(lengths(lst)))) res # cost customer.satisfaction safety time #1 40 57 32 24 #2 38 72 30 40 #3 36 73 58 22 8 … Read more

[Solved] first occurrence of age in a nuggets eating contest

We can use data.table to get the fastest extraction using either unique with by option unique(df2, by = “Person”) Or extracting with row index setDT(df2)[df2[, .I[1L],Person]$V1] Update If we need the minimum ‘Age’ row per ‘Person setDT(df2)[, .SD[which.min(Age)], Person] Or if we prefer dplyr, then library(dplyr) df2 %>% group_by(Person) %>% slice(1L) Update df2 %>% group_by(Person) … Read more

[Solved] R package equivalent to Matlab’s gmdistribution.fit()

The MClust package contains the function densityMclust which produces an object that contains parameter estimates for the fitted Gaussian mixture model along with the density itself. From the MClust manual: > densWaiting <- densityMclust(faithful$waiting) > summary(densWaiting, parameters = TRUE) ——————————————————- Density estimation via Gaussian finite mixture modeling ——————————————————- Mclust E (univariate, equal variance) model with … Read more

[Solved] how to plot with ggplot?

you should not use all variables as id.vars try this code dfm <- melt(df, id.vars=c(“iteration”)) What do you really need to plot?? What is your x and y axis? line plot or point? As far as i understood this should work. ggplot(dfm,aes(iterartion,value,colour=variable))+geom_point() solved how to plot with ggplot?

[Solved] Creating Index Variable in R [duplicate]

Do you just want to code the table up? Would something like this suffice?: PCP <- c(0, 4.9, 5, 9.9, 10, 14.9, 15) seq2max <- seq(0,max(PCP)+5,5) result <- data.frame(min=seq2max,max=seq2max+4.9,DRI=seq_along(seq2max)-1) min max DRI 1 0 4.9 0 2 5 9.9 1 3 10 14.9 2 4 15 19.9 3 5 20 24.9 4 result$DRI # [1] … Read more