Extract Last Element in an R Matrix

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:40:07

738 Views

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

Extract Data Frame Columns Stored in a List in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:35:36

3K+ Views

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

Remove Starting and Ending Zeros in an R Vector

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:31:54

649 Views

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

Remove Rows Containing At Least One Zero in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:29:13

3K+ Views

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

Split Data Frame Using Row Number in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:25:37

2K+ Views

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

Check if a Vector Exists in a List in R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:17:43

5K+ Views

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

Display Mean in a Boxplot with Cross Sign in Base R

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:14:20

338 Views

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

Create a Simple Screen Using Tkinter

Prasad Naik
Updated on 16-Mar-2021 11:10:50

543 Views

 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

Select Columns in R Without Missing Values

Nizamuddin Siddiqui
Updated on 16-Mar-2021 11:09:52

3K+ Views

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

Display Various DateTime Formats in Python

Prasad Naik
Updated on 16-Mar-2021 11:09:09

286 Views

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

Advertisements