[Solved] Object not found in for loop

[ad_1] The problem in the peace of code below after your definition of crra function: eua = c(pa1*crra(a1,r)+pa2*crra(a2,r)) eub = c(pb1*crra(b1,r)+pb2*crra(b2,r)) Basically you are trying to use r variable before it’s defined moreover it is a duplicate of the code inside the for-loop. If you comment out these two lines everything goes OK. Please see … Read more

[Solved] Ifelse to Compare the content of columns with dplyr

[ad_1] The best way to do this would be to use the glue package. This allows you to easily assign variable names based on strings: library(tidyverse) library(glue) > df <- data.frame( sample_id = c(‘SB013’, ‘SB014’, ‘SB013’, ‘SB014’, ‘SB015’, ‘SB016’, ‘SB016’), IN = c(1,2,3,4,5,6,7), OUT = c(rep(‘out’,7))) > df sample_id IN OUT 1 SB013 1 out … Read more

[Solved] How could I get a graph have a shade (confidence interval), legend have no a shade [closed]

[ad_1] First I loaded ggplot and added mtcars library to explain. library(ggplot2) cars <- mtcars This is a normal ggplot graph with geom_smooth and the confidence interval. ggplot(cars, aes(x = mpg, y= disp)) + geom_point() + geom_smooth() within geom_smooth is an option se which is your confidence interval. se is default set to TRUE by … Read more

[Solved] adding each element each row of two columns together in the same file without loops in R

[ad_1] A summary of options suggested in the comments (and some others): dd<-data.frame( A= 1:3, B= 2:4 ) You can get the sum of the columns with dd$A + dd$B rowSums(dd) with(dd, A + B) dd[,1]+ dd[,2] dd[,”A”]+ dd[,”B”] apply(dd, 1, sum) do.call(‘+’, dd) Reduce(“+”,dd) [ad_2] solved adding each element each row of two columns … Read more

[Solved] Remove duplicate on multiple columns keep newest [closed]

[ad_1] In R, using dplyr: data %>% group_by(Name, CoordinateX, CoordinateY) %>% arrange(desc(Date)) %>% distinct() %>% ungroup() Give the output: Name Date CoordinateX CoordinateY Aaa 2018-08-29 650000 134999 Bbb 2010-08-29 650000 134999 Bbb 2010-08-29 655600 134999 Ccc 2010-08-29 655600 134999 [ad_2] solved Remove duplicate on multiple columns keep newest [closed]

[Solved] Running jobs in background in R

[ad_1] What would help is to output it to a file when you have computed it and then parse that file everytime you open R. Write yourself a computeMatrix() function or script to produce a file with the matrix stored in a sensible format. Also write yourself a loadMatrix() function or script to read in … Read more

[Solved] How to get date difference if column is NA [closed]

[ad_1] In the future, please follow the questions guidelines. library(lubridate) start = as.Date(c(“14.01.2015”, “26.03.2015”),format = “%d.%m.%Y”) end = as.Date(c(“18.01.2015”, NA),format = “%d.%m.%Y”) diff = ifelse(!is.na(end),difftime(end,start,units=”days”),difftime(Sys.time(),start,units=”days”)) df = data.frame(start,end,diff) View(df) start end diff 1 2015-01-14 2015-01-18 4.0000 2 2015-03-26 NA 174.7846 [ad_2] solved How to get date difference if column is NA [closed]

[Solved] Substitute LHS of = in R

[ad_1] 1) eval/parse Create a cmd string, parse it and evaluate it: f2 <- function(DF, x, env = parent.frame()){ cmd <- sprintf(“mutate(%s, %s = mean(v1))”, deparse(substitute(DF)), x) eval(parse(text = cmd), env) } f2(DF, “v1_name”) giving v1 v1_mean 1 1 2 2 2 2 3 3 2 … etc … 2) eval/as.call Another way is to … Read more

[Solved] Matching Data Tables by five columns to change a value in another column

[ad_1] In R it is always preferable to avoid loops wherever possible, as they are usually much slower than alternative vectorized solutions. This operation can be done with a data.table join. Basically, when you run dt1[dt2]; you are performing a right-join between the two data.tables. The preset key columns of dt1 determine which columns to … Read more

[Solved] How do I achieve a bubbleplot? [closed]

[ad_1] To achieve this you could use ggplot. Here an example: require(ggplot2) df <- data.frame(x = c(-2, -1.5, 1, 2, 3), y = c(-1, 0.8, 1, 1, 2), country = c(“SWI”, “FRA”, “US”, “UK”, “NL”), size = c(15,12,20,4,7)) ggplot(df, aes(x = x, y = y)) + geom_point(aes(size = size), pch=1) +geom_text(aes(label=country),hjust=-0.5, vjust=0) + xlim(c(-3,4)) + … Read more

[Solved] function that returns a value from column b when specifying a value from column a [closed]

[ad_1] We can use == exampledataframe$X2[exampledataframe$X1==”A”] As a function fun1 <- function(data, Var1, Var2, val){ data[[Var2]][data[[Var1]]==val] } fun1(exampledataframe, “X1”, “X2”, “B”) #[1] “4” data exampledataframe <- data.frame(X1= c(“A”, 1, “B”, 2, “C”), X2= c(3, “D”, 4, “F”, 5), stringsAsFactors=FALSE) 0 [ad_2] solved function that returns a value from column b when specifying a value from … Read more

[Solved] R: transforming one column to another [closed]

[ad_1] This should work if your logic is whenever there is 1 in column x fill y with 1 forward until there is -1 in column x from where fill y with 0 and vice versa: Since you are trying to fill forward vectors with previous values, you may want to use na.locf(last observation carried … Read more