Filter Single Column of a Matrix with Column Name in R

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:25:51

2K+ Views

To filter a single column of a matrix in R if the matrix has column names, we can simply use single square brackets but this will result in a vector without the column name. If we want to use the column name then column name or column number needs to be passed with drop=FALSE argument as shown in the below examples.Example1Live Demo> M1 colnames(M1) M1Output      V1 V2 V3 V4 [1, ]  0  0  1  0 [2, ]  1  1  1  1 [3, ]  0  0  0  0 [4, ]  0  1  1  0 [5, ]  1  1  1 ... Read More

Truncate Character Vector with Three Dots After N Characters in R

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:21:59

237 Views

To truncate character vector with three dots after n characters can be done with the help of str_trunc function of stringr package. For example, if we have a character vector say x and each value containing 10 characters then truncating those values with three dots after 5 characters can be done by using the command str_trunc(x, 8).Example1Live Demo> x1 x1Output[1] "rstuvwxyz" "rstuvwxyz" "abcbefgh" "rstuvwxyz" "ijklmnopq" "ijklmnopq" [7] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "abcbefgh" [13] "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" "ijklmnopq" "ijklmnopq" [19] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" [25] "ijklmnopq" "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" [31] "rstuvwxyz" "abcbefgh" "abcbefgh" "rstuvwxyz" "rstuvwxyz" ... Read More

Assign Column Value in Data Frame Based on Another Column in R

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:18:25

5K+ Views

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 ... Read More

Find Mean for Multiple Columns in R Data Frame

Nizamuddin Siddiqui
Updated on 04-Mar-2021 19:15:04

351 Views

The easiest way to find the column means of an R data frame is to use colMeans function but if we do not want to use it then it can be done with the help of sapply. While using sapply, we need to make sure that we are selecting only numerical columns of the data frame. Check out the below examples to understand how it works.Example1Consider the CO2 data frame in base R −Live Demo> head(CO2, 20)Output  Plant  Type  Treatment  conc  uptake 1   Qn1 Quebec nonchilled  95   16.0 2   Qn1 Quebec nonchilled  175  30.4 3   Qn1 ... Read More

Remove Rows in a Data Table Object with NA's in R

Nizamuddin Siddiqui
Updated on 04-Mar-2021 18:29:49

2K+ Views

If a row contains missing values then their sum will not finite, therefore, we can use is.finite function with the data.table object to remove the rows with NA’s. For example, if we have a data.table object called DT that contains some rows with NA’s then the removal of those rows can be done by using DT[is.finite(rowSums(DT))].Example1Loading data.table package and creating a data.table object −> library(data.table) > x1 x2 DT1 DT1Output   x1  x2 1:  1   2 2:  NA  4 3:  1   2 4:  NA  5 5:  1   6 6:  1   8 7:  NA  3 8:  1   ... Read More

Create Boxplot with Multiple Factor Levels using ggplot2 in R

Nizamuddin Siddiqui
Updated on 04-Mar-2021 18:23:00

5K+ Views

To create a boxplot, we have one factor and one numerical column and the boxplot is created for each category or levels in that factor. Now if we have two factors then the boxplot can be created for both factor levels by passing fill argument in geom_boxplot. This will help us to differentiate between the boxplots for the two factors. Check out the below examples to understand how it works.ExampleConsider the below data frame −Live Demo> x y grp df dfOutput       x              y  grp 1 Female    0.790349405 b ... Read More

Generate Tkinter Buttons Dynamically

Dev Prakash Sharma
Updated on 04-Mar-2021 14:11:15

2K+ Views

In this article, we will see how to create buttons dynamically in a tkinter window. Creating buttons dynamically means customizing the buttons and their functionality by adding events to them.First, we will import the tkinter library in the notebook, then we will create an instance using the Button function which takes parameters such as parent or root of the window, textvariable which is the value to assign in each button and command.SyntaxButton(parent, textvariable, command)Examplefrom tkinter import * import tkinter as tk # create an instance of tkinter win = tk.Tk() #Define the size of the window win.geometry("700x200") ... Read More

Extract Wikipedia Data in Python

Dev Prakash Sharma
Updated on 04-Mar-2021 14:09:24

1K+ Views

In this article, we will see how to extract Wikipedia data using Python. Python is widely used for creating web scrapers to capture the meta information from the websites.For this article, we will use the Wikipedia API and library to get the data from the Wikipedia source URL. The API will help to fetch the data from the given URL. Then, we will invoke the method on the given URL and print the information on the screen.In order to extract data from Wikipedia, we have to first import the wikipedia library in Python using 'pip install wikipedia'.In this program, we ... Read More

Dynamically Resize Button Text in Tkinter

Dev Prakash Sharma
Updated on 04-Mar-2021 14:08:19

1K+ Views

Let us suppose we have created a button and a label in Tkinter Frame. The task is to allow the button text to dynamically resize to its main window. We can create the button using a button widget. However, there are several other functions used to create the button label dynamically.In this example, we will create two buttons with some labels in it. By using the Grid method such as rowconfigure() and columnconfigure(), we will dynamically resize the main window or the root.To make the button text dynamic, we will use the bind(, command) method which will help us to ... Read More

Detect Changes in OptionMenu or Checkbutton in Tkinter

Dev Prakash Sharma
Updated on 04-Mar-2021 14:06:52

2K+ Views

Let us suppose that in a particular application, we have some fixed set of options or choices for the user in a drop-down list. The Options or Choices can be created using the OptionMenu Widget Constructor.OptionMenu(window, variable, choice1, choice2, choice3……)Once the option is created, it can be detected by a click event which generally prints whether a particular option is selected or not. For this example, we will simply create an application where a check button will be present with some choices from the range (1 to 9). By default, the button is set to “1” using the set method. ... Read More

Advertisements