[Solved] Calculate conditional mean in R with dplyr (like group by in SQL) [duplicate]

I think what you are looking for (if you want to use dplyr) is a combination of the functions group_byand mutate. library(dplyr) city <- c(“a”, “a”, “b”, “b”, “c”) temp <- 1:5 df <- data.frame(city, temp) df %>% group_by(city) %>% mutate(mean(temp)) Which would output: city temp mean(temp) (fctr) (int) (dbl) 1 a 1 1.5 2 … Read more

[Solved] Grouping and summarizing [closed]

Editing from previous wrong answer and borrowing from @akron for the use of rle, you can do this: assuming that your data is in a data.frame named “df” and your “frame classes” are in a column named “frame_class”, as in the code below, this should work: df = data.frame(n_frame = seq(1:13), frame_type = “frame_type”, frame_class … Read more

[Solved] Could you please help me to understand an R code?

First of all, in general, var <- expr evaluates the R expression expr and assigns the result to the variable var. If the statement occurs inside a function, then var becomes a function-local variable, otherwise, it becomes a global variable. c(0,0,0,1,0,1,0,1,1,1,1,0) Combines 12 double literals into a double vector in the order given. matrix(c(0,0,0,1,0,1,0,1,1,1,1,0),4,3, byrow=T … Read more

[Solved] Take one sample per row at a time, compute mean, loop [closed]

First lets make so data to experiment with set.seed(100) example <- matrix(runif(47*30), nrow = 47) example[sample(length(example), 250)] <- NA Now we can calculate our means. The apply function samples a random value from each row (!is.na excludes NA values), mean gets the mean, and replicate repeats this 10000 times. exmeans <- replicate(10000, mean(apply(example, 1, function(x) … Read more

[Solved] P value of R t.test() function [closed]

From ?t.test – my emphasis: alternative: a character string specifying the alternative hypothesis, must be one of ‘”two.sided”‘ (default), ‘”greater”‘ or ‘”less”‘. You can specify just the initial letter. 1 solved P value of R t.test() function [closed]

[Solved] Plot euclidian points in R from data frame [closed]

Um, you can just call the plot function. A sample matrix: data <- cbind(x = 1:10, y = runif(10)) class(data) ## [1] “matrix” plot(data) This also works for a data frame. data <- data.frame(x = 1:10, y = runif(10)) plot(data) In general, (where there are more than two columns), you usually want to use with. … Read more

[Solved] Eclipse and R on Mac [closed]

The StatET package for Eclipse works great on a Mac. Go to StatET and follow his installation instructions. Sometimes it can be a little tricky to set up once the package is installed in Eclipse. There are several cheat sheets in Eclipse to assist with the setup. Look for them under Help->Cheat Sheets in Eclipse. … Read more

[Solved] how to convert str to int?

read.table returns a data frame (as per the documentation), which is not the same thing as a matrix. As I’m sure you’re aware, your data had three columns, only the second two being numeric. When you convert the data frame from read.table to a matrix with as.matrix R coerces everything to a single type. This … Read more

[Solved] r – merge is only working if I do it twice?

From the documentation of merge: If the columns in the data frames not used in merging have any common names, these have suffixes (“.x” and “.y” by default) appended to try to make the names of the result unique. If this is not possible, an error is thrown. Based on your names results before and … Read more

[Solved] Plot 6-D into 2-D in r [closed]

The simplest thing would be to use PCA to reduce the dimensionality of your data to 2 or 3 dimensions. k-means clustering ought to assign a group to each row of your data so you can easily plot the different groups on the reduced dataset. Here’s a simple way to do PCA though you could … Read more

[Solved] Add leading zeros before first hypen [closed]

We can use sub to extract the numbers before the first – by matching the – followed by one or more characters (.*) till the end of the string, replace it with “”, convert it to numeric (as.numeric), as well as extract the substring from the first instance of – till the end of the … Read more

[Solved] programming R using ifelse with multiple conditions

You can avoid the loop or the apply statement by vectorizing the ifelse statement. If you define i as the vector from 3:length(E$phone), you can then run the ifelse statement directly. #test data phone<-c(123,123,123,333,333,333,456,456,456,789,789,789,500,500,500) time<-c(“2018-11-06″,”2018-11-06″,”2018-11-06″,”2018-11-09″,”2018-11-09″,”2018-11-09”, “2018-11-07″,”2018-11-07″,”2018-11-07″,”2018-11-05″,”2018-11-05”, “2018-11-05”, “2018-11-06″,”2018-11-06″,”2018-11-06”) time<-as.Date(time) tel<-c(0,0,1,1,0,1,1,1,0,1,1,1,0,0,1) porad<-c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3) E<-data.frame(phone, time, tel, porad) E$de[1]=ifelse(E$phone[1]==E$phone[2] & E$time[1]==E$time[2] & E$porad[1]==2 & E$tel[1]==1,1,0) E$de[2]=ifelse(E$phone[2]==E$phone[3] & E$time[2]==E$time[3] … Read more