To extract the last element in an R matrix, we can use the length function along with single square brackets that are used for subsetting. For example, if we have a matrix called M as shown below −M1 2 3 4 5 6 7 8 9then we can extract last value of M by using the command M[length(M)].ExampleConsider the below matrix − Live DemoM1
Suppose we have two frames each having 5 columns that are stored in a list in R and the data that belongs to same columns has some kind of inherent relationship or we want to check whether there exists a relationship between them then we might want to extract those columns. Therefore, we can use lapply function for this extraction. For example, if we have a list called LIST that store two data frames then column 3 of each data frame can be extracted by using the command lapply(LIST, "[", 3).ExampleConsider the below data frames and list of these data ... Read More
To remove starting and ending zeros in an R vector, we can use min and max function to access values except 0 and then subsetting with single square brackets. For example, if we have a vector called x then we can remove starting and ending zeros by using the command −x[min(which(x!=0)):max(which(x!=0))]Example Live Demox1
To remove rows that contain at least one 0, we can use single square brackets for subsetting rows with apply that will select rows that do not contain even one zero. For example, if we have a data frame called df then we can remove rows that contain at least one 0 can be done by using the command df[apply(df,1, function(x) all(x!=0)),].ExampleConsider the below data frame − Live Demox1
To split a data frame using row number, we can use split function and cumsum function. The split function will split the rows and cumsum function will select the rows. For example, if we have a data frame called df that contains twenty rows then we can split into two data frames at row 11 by using the below command −split(df,cumsum(1:nrow(df)%in%11)).ExampleConsider the below data frame − Live Demox1
To check if a vector exists in a list, we can use %in%, and read the vector as list using list function. For example, if we have a list called LIST and a vector called V then we can check whether V exists in LIST using the command LIST %in% list(V).ExampleConsider the below list − Live DemoList
To display mean in a boxplot with cross sign in base R, we can use the points function and pass the mean with pch = 4 that represents a star, also we can change the color to highlight the mean using col argument and the size of the start can be changed using lwd argument as shown in the below examples.Example Live Demox
We will create a simple screen using the Tkinter library.AlgorithmStep 1: Import tkinter. Step 2: Create an object of the tkinter class. Step 3: Display the screen.Example Codeimport tkinter as tk window = tk.Tk()Output
There are two easy methods to select columns of an R data frame without missing values, first one results in a vector and other returns a matrix. For example, if we have a data frame called df then the first method can be used as df[,colSums(is.na(df))==0] and the second method will be used as t(na.omit(t(df))).ExampleConsider the below data frame − Live Demodf1
The datetime module supplies classes for manipulating dates and time. We will display different formats like day of the week, week number, day of the year, etc.AlgorithmStep 1: Import datetime. Step 2: Print day of the week. Step 3: Print week number. Step 4: Print day of the year.Example CodeLive Demoimport datetime print("Day of the week: ", datetime.date.today().strftime("%A")) print("Week number: ", datetime.date.today().strftime("%W")) print("Day of the year: ", datetime.date.today().strftime("%j"))OutputDay of the week: Sunday Week number: 06 Day of the year: 045ExplanationThe arguments of the strftime() function are explained below:%A: Weekday's full name (Example: 'Monday')%W: Week number of the year with ... Read More