Posts

Showing posts with the label r

Calculating conditional cumulative time

Calculating conditional cumulative time Following the pointers from this question. I'd like to calculate the cumulative time for all the Cat s, by considering their respective last toggle status. Cat EDIT: I'd also want to check if the FIRST Toggle status of a Cat is Off and if it is so, for that specific cat , the time from midnight 00:00:00 till this first FIRST Off time should be added to its total conditional cumulative ontime. EDIT: Toggle Cat Off cat 00:00:00 Sample data: Time Cat Toggle 1 05:12:09 36 On 2 05:12:12 26R Off # First Toggle of this Cat happens to be Off, Condition met 3 05:12:15 26R On 4 05:12:16 26R Off 5 05:12:18 99 Off # Condition met 6 05:12:18 99 On 7 05:12:24 36 Off 8 05:12:26 36 On 9 05:12:29 80 Off # Condition met 10 05:12:30 99 Off 11 05:12:31 95 Off # Condition met 12 05:12:32 36 Off Desired sample output: Cat Time(Secs) 1 36 21 2 26R 18733 # (=1+18732), 18732 secs to be added = total Sec from midnight till 05:12:12...

Installing PDFtotext in R

Installing PDFtotext in R I am trying to run the PDFtotext package in R. PDFtotext When I run these commands : library(tm) pdf=readPDF(control=list(text="-layout"))(elem=list(uri=uri), language="en", id="idi") I get this error : Error in system2("pdftotext", c(control$text, shQuote(x), "-"), stdout = TRUE) : "pdftotext" not found In addition: Warning message: running command "pdfinfo" "C:*****NCLR AR 2005.pdf" had status 127 Does anyone know what the problem might be? Sys.which("pdftotext") is "" ? I.e. the file is not found. Have you installed it? You may want to try the package pdftools as an alternative to read pdfs. – lukeA Apr 6 '16 at 14:38 Sys.which("pdftotext") "" pdftools ...

Error in effect.default…argument “offset” is missing, with no default

Error in effect.default…argument “offset” is missing, with no default I'm looking to plot the effects for a TOBIT regression model at -1SD and +1SD using a bar graph. I would normally use a line graph, but my co-authors have asked for a bar graph instead. I have had some help identifying what I should do here, but I am getting an error that no one seems to be able to figure out. Creating dataframe - this runs fine. tobitforgraph <- survreg(Surv(S1, S2, type='right') ~ +(Var1centered)*Var2centered*Var3standardized,data=Datafilename, dist='gaussian', robust=TRUE)) Load effects library. library(effects) Extract values for plotting (the values coming from here are then supposed to be used to plot as normal in Excel, although there may be a way to do it in R; regardless, this is where I am getting an error). print((intplot<-as.data.frame(ef< Effect(c("Var1centered","Var3standardized"),mod=tobitforgraph,xlevels=list(Var1centered=c(-.4987531,.5...

Increase from previous value by condition in R [duplicate]

Increase from previous value by condition in R [duplicate] This question already has an answer here: I was searching for an answer to my specific problem, but I didn't find a conclusion. I have a dataframe with data ID a 1 0 2 0 3 1 4 1 5 1 6 1 7 0 8 1 9 1 10 0 11 1 12 0 13 0 Now i want to add "b" column with number increase from previous b if a == 1 Result like this ID a b 1 0 0 2 0 0 3 1 1 4 1 2 5 1 3 6 1 4 7 0 0 8 1 1 9 1 2 10 0 0 11 1 1 12 0 0 13 0 0 14 1 1 15 1 2 16 1 3 17 1 4 Thanks in advance! This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 3 Answers 3 Using dplyr an option can be to group on cumsum(a==0) . This will create a group which got a previous row (if available with a=0 for all rows with a=...

Adding text annotation to a clustering scatter plot (tSNE)

Image
Adding text annotation to a clustering scatter plot (tSNE) I have XY data (a 2D tSNE embedding of high dimensional data) which I'd like to scatter plot . The data are assigned to several cluster s, so I'd like to color code the points by cluster and then add a single label for each cluster , that has the same color coding as the cluster s, and is located outside (as much as possible) from the cluster 's points. XY tSNE scatter plot cluster cluster cluster cluster cluster Any idea how to do this using R in either ggplot2 and ggrepel or plotly ? R ggplot2 ggrepel plotly Here's the example data (the XY coordinates and cluster assignments are in df and the labels in label.df ) and the ggplot2 part of it: XY cluster df label.df ggplot2 library(dplyr) library(ggplot2) set.seed(1) df <- do.call(rbind,lapply(seq(1,20,4),function(i) data.frame(x=rnorm(50,mean=i,sd=1),y=rnorm(50,mean=i,sd=1),cluster=i))) df$cluster <- factor(df$cluster) label.df <- data.frame(c...

Reading multiple xlsx files each with multiple sheets - purrr

Reading multiple xlsx files each with multiple sheets - purrr I have multiple excel files, each with different worksheets. I have tried to use readxl and map to import it to R. However, I was only able to do it using a for loop. The code below works fine but I would like to know if there is a clever way to do this. I keep thinking that I could have done it with map2 but I am missing something. library(tidyverse) library(readxl) library(writexl) ### As a first step, I get all the files from my project folder and create an empty list for looping purposes files <- list.files(pattern = ".xlsx") data_xlsx <- list() ### I then use seq_along in all the files and map_df to read the each excel file for (i in seq_along(files)) { data_xlsx[[i]] <- files[i] %>% excel_sheets() %>% set_names() %>% map_df( ~ read_xlsx(path = files[i], sheet = .x, range = "H3"), .id = "sheet") } # I use the code below to get the files name into the list dat...

How to zoom in on a specific range of values for a categorical variable in ggplot2?

Image
How to zoom in on a specific range of values for a categorical variable in ggplot2? I just want to zoom-in on the x axis between the values ford and nissan in the mpg dataframe. packageused: tidyverse But I am getting the following error when using coord_cartesian() function p<-ggplot(mpg,aes(x=manufacturer,y=class)) p+geom_point()+ + coord_cartesian(xlim = c('ford','nissan')) Error in +coord_cartesian(xlim = c("ford", "nissan")) : invalid argument to unary operator 1 Answer 1 You can use a function for contextual zoom from ggforce package ( facet_zoom ) to achieve this: ggforce facet_zoom # loading needed libraries library(ggplot2) library(ggforce) # selecting variables to display names <- as.vector(unique(mpg$manufacturer)) selected.names <- names[4:11] # zooming in on the axes ggplot(mpg, aes(x = manufacturer, y = class)) + geom_...

How can I change the thickness of a specific line and/or add shape in a multi-line plot?

Image
How can I change the thickness of a specific line and/or add shape in a multi-line plot? I have a data called molten.data below. I have this code which plots the lines I want, but I need to change the thickness of one specific line ( G11F:G11M ) to make it look thicker compared to other lines or preferably add shape to the data point in that line. How can we do it? molten.data G11F:G11M code I have: ggplot(molten.data, aes(variable, value,group= key.related,colour=key.related)) + geom_line() + geom_point() data: molten.data<- structure(list(key.related = c("G11F:G11F", "G11F:G11F", "G11F:G11F", "G11F:G11M", "G11F:G11F", "G11F:G11M", "G11F:AOGC-02-0079", "G11F:G11F", "G11F:G11M"), variable = structure(c(1L, 2L, 3L, 3L, 4L, 4L, 4L, 5L, 5L), .Label = c("IBS_2_samples", "IBS_4_samples", "IBS_8_samples", "IBS_16_samples", "IBS_32_samples"), ...

Merge two columns into one, element by element in R

Merge two columns into one, element by element in R I have the following data: df <- data.frame(estimate = c(1,2), sd = c("(0.5)*","(0.5)***")) rownames(df) <- c("race","gender") I want to make a journal quality table by merging the two columns into one, element by element. The desired output would be: Results race 1 (0.5)* gender 2 (0.5)*** try m1 <- matrix(c(t(df)));row.names(m1) <- c(rbind(row.names(df), "")) – akrun Jul 1 at 1:23 m1 <- matrix(c(t(df)));row.names(m1) <- c(rbind(row.names(df), "")) Awesome! Thanks for such a beautiful solution. – Leonhardt Guass Jul 1 at 1:25 ...

R legend pch mix of character and numeric

Image
R legend pch mix of character and numeric Is it possible to use a mix of character and number as plotting symbols in R legend? plot(x=c(2,4,8),y=c(5,4,2),pch=16) points(x=c(3,5),y=c(2,4),pch="+") legend(7,4.5,pch=c("+",16),legend=c("A","B")) #This is the problem 5 Answers 5 My first thought is to plot the legend twice, once to print the character symbols and once to print the numeric ones: plot(x=c(2,4,8),y=c(5,4,2),pch=16) points(x=c(3,5),y=c(2,4),pch="+") legend(7,4.5,pch=c(NA,16),legend=c("A","B")) # NA means don't plot pt. character legend(7,4.5,pch=c("+",NA),legend=c("A","B")) NOTE: Oddly, this works in R's native graphical device (on Windows) and in pdf() , but not in bmp() or png() devices ... pdf() bmp() png() Hi, Thanks for the reply. Yes This was actual...

pull out p-values and r-squared from a linear regression

pull out p-values and r-squared from a linear regression How do you pull out the p-value (for the significance of the coefficient of the single explanatory variable being non-zero) and R-squared value from a simple linear regression model? For example... x = cumsum(c(0, runif(100, -1, +1))) y = cumsum(c(0, runif(100, -1, +1))) fit = lm(y ~ x) summary(fit) I know that summary(fit) displays the p-value and R-squared value, but I want to be able to stick these into other variables. summary(fit) It only displays the values if you don't assign the output to an object (e.g. r <- summary(lm(rnorm(10)~runif(10))) does not display anything). – Joshua Ulrich Apr 7 '11 at 21:35 r <- summary(lm(rnorm(10)~runif(10))) 11 Answers 11 r-squared : You can...

Assign a dynamic function to Global Environment in R

Image
Assign a dynamic function to Global Environment in R This question will be a simple one for some members of this forum, but I am expending a lot of time searching for an answer and I did not find any working one. Let me clarify: I have a simple function that conducts a linear regression, and the results become a variable and this variable is assigned to the global environment. Straightforward method. However, every time I run this function, the variable is replaced, what I don't want. I just want to define a specific name instead of replacing this variable. Please check out this code to understand my question: set.seed(123) ds <- data.frame(income=rnorm(1000,700,20), sex=c(0,1), age=rnorm(1000,30,10)) regress <- function(iv_string) { regression_formula <- as.formula(paste("income ~", iv_string)) results <<- lm(regression_formula, ds) plot(results) print(results) } regress("age") regress("sex") Something like that regress <-...

Using tapply when you have different n for each treatment

Using tapply when you have different n for each treatment I am an R beginner trying to fit my data to a non-linear regression. Specifically I want to fit the rate of insect development over different temperatures to a power function. Below is my code, modified from the examples at the bottom of this page: https://docs.tibco.com/pub/enterprise-runtime-for-R/3.1.0/doc/html/Language_Reference/stats/selfStart.html You should be able to paste the code below right into R to get what I get. structure(list(Larvae_temp = c(10L, 10L, 10L, 10L, 10L, 10L), Larvae_rate = c(0.047757234, 0.04174518, 0.04174518, 0.04174518, 0.04174518, 0.04174518)), .Names = c("Larvae_temp", "Larvae_rate"), row.names = c(NA, 6L), class = "data.frame") SSpower<-(selfStart(~A*(Larvae_temp^B), initial=function(mCall,data,LHS) {xy<-sortedXyData(mCall[["Larvae_temp"]],mCall[["Larvae_rate"]],LarvaeDevelopment) ##I think the error...

How can I use LOOCV in R with KNN? [on hold]

Image
How can I use LOOCV in R with KNN? [on hold] I am trying to use KNN with cancer data. At first, I only used separation data into train and test set, but I got unexpected results. So I want to use LOOCV to make sure. I found only LOOCV with generalized linear models. such as glm.fit = glm(mpg ~ horsepower, data=Auto) glm.fit = glm(mpg ~ horsepower, data=Auto) So how can I use LOOCV in R with KNN? EDIT My code wdbc<- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/wdbc.data",sep=",",stringsAsFactors = FALSE) wdbc<-wdbc[-1] normalize <- function(x) {return ((x-min(x)) / (max(x) - min(x)))} wdbc_n <- as.data.frame(lapply(wdbc[2:31], normalize)) wdbc_train<-wdbc_n[1:469,] wdbc_test<-wdbc_n[470:569,] I uploaded the data and I excluded the first column which is the class label. Then I separated the data into train and test set. However, I want to use LOOCV in the separation instead of my separation above. Pleas...