Found 33676 Articles for Programming

How to remove starting and ending zeros in an R vector?

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

652 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

How to remove rows that contain at least one 0 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

How to split a 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

How to 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

How to 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

How to create a simple screen using Tkinter?

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

548 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

Python program to display various datetime formats

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

287 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

How to 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

How to display tick marks on upper as well as right side of the plot using ggplot2 in R?

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

755 Views

To display tick marks on upper as well as right side of the plot, we can create duplicate axes for X as well Y by using scale_x_continuous and scale_y_continuous functions. The argument that will help us in this case is sec.axis and we need to set it to dup_axis as scale_x_continuous(sec.axis=dup_axis()) and scale_y_continuous(sec.axis=dup_axis()). Check out the below example to understand how it can be done.ExampleConsider the below data frame − Live Demox

Adding textures to graphs using Matplotlib

Prasad Naik
Updated on 16-Mar-2021 11:07:40

595 Views

In this program, we will plot a bar graph using the matplotlib library. The most important Step in solving matplotlib related problems using the matplotlib library is importing the matplotlib library. The syntax is:import matplotlib.pyplot as pltPyplot is a collection of command style functions that make Matplotlib work like MATLAB. In addition to plotting the bar graphs, we will also add some textures to the graphs. The 'hatch' parameter in the bar() function is used to define the texture of the barAlgorithmStep 1: Define a list of values. Step 2: Use the bar() function and define parameters like xaxis, yaxis, ... Read More

Advertisements