Finding the last entry

 Let's say we have a table like below. The same product run on the same machine more than one time, re-work or re-processed.




What we are interested in the last run. 


Using base command aggregate 

laste_entry_max <- aggregate(RUN_Number ~ ID, last_entry, max)




By doing a merge you can do the subset the table.

laste_entry_max_all_c <- merge(laste_entry_max, last_entry, by = c('ID', 'RUN_Number'), all.x = T) 



If the run number information is not there we can use Time_Stamp


You can also use dplyr package

library(dplyr) 

laste_entry_max <- last_entry %>%  

                      group_by(ID) %>% 

                          slice(which.max(RUN_Number)) 



Creating empty data frame

The easiest way to create a null object.

We can use {} to create a NULL object

Simple example, use in for loop.


df <- {}

for (i in 1:10){

  tbl <- c(paste('loop', i, sep='-'))

  df <- rbind(df,tbl)

}


End result is like this,



But if you want to define the columns,


df <- data.frame(matrix(ncol = 3, nrow = 0)) 

colnames(df) <- c("name", "school", "grade")







Handling big numbers in R

One of the issue with R big (long numbers). You can find very detail explanation on what is the issue. But in this post we discuss in a very simple way. Mainly how to work around when faced with this issue.

A simple example

Let's say need to read text file with varying length numbers.


number
1234567891234567
123456789123456789
9988776655443322111111
54767686587697697696679565664656
2423546567899009052342313244568798978
1000000000000000006666666666660000000000000003333333333


Using red csv

aa <- read.csv("C:\\data\\rblog\\number.txt")


What we get is this.



Checking the length....

nchar(aa$number)
[1] 16 18 20 20 20  5


The numbers are automatically changed to scientific formatting.

To avoid scientific formatting you can use,

options(scipen=999)






First looks it seems to be OK, but if you look at the last digits you can see they are changed, except for the first row.


To avoid this we need to force the data as character. 

aa <- read.csv("C:\\data\\rblog\\number.txt",  colClasses= 'character')

Then we get.




If we force the character to convert to numeric,

aa$number <- as.numeric(aa$number)

The issue of changing last digits will reappear.