Server Side Programming Articles - Page 1214 of 2650

How to subset an R data frame based on string match in two columns with OR condition?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:42:33

1K+ Views

To subset an R data frame based on string match in two columns with OR condition, we can use grepl function with double square brackets and OR operator |. For example, if we have a data frame called df that contains two string columns say x and y then subsetting based on a particular string match in any of the columns can be done by using the belowSyntaxdf[grepl("text",df[["x"]])|grepl("text",df[["y"]]),]Check out the below examples to understand how it works.Example1Consider the below data frame − Live Demof1

How to change default point size of geom_point in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:37:41

451 Views

To change the default point size of geom_point, we need to use update_geom_defaults function. Specifically, for the change of point size the syntax will be as follows −update_geom_defaults("point",list(size=”value”))Here, we can change the value according to our need.ExampleConsider the below data frame − Live Demox

How to find the proportion of each value for a cross tab obtained from a data frame in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:33:10

988 Views

To find the proportion of each value for a cross tab obtained from a data frame, we can use prop.table function. Suppose we have a data frame called df that contains three columns, two categorical say C1 and C2 and one numerical say Y then the cross tab will be created by using the command xtabs(Y~.,df). Now the proportion of each value can be found by using prop.table(xtabs(Y~.,df),1).Example1Consider the below data frame − Live Demof1

How to change the point size in geom_point conditionally in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:24:20

2K+ Views

To change the point size in geom_point conditionally, we can define the condition in geom_point with aes and the size using scale_size_manual function of ggplot2 package. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot with different size of points for x values greater than 5 and less than equal to 5 can be drawn by using the below command −ggplot(df, aes(x, y))+geom_point(aes(size=x>5))+scale_size_manual(values=c(4, 7))ExampleConsider the below data frame − Live Demox6))+scale_size_manual(values=c(4, 7)) OutputRead More

How to create a list of regression models for predefined vectors in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:23:20

2K+ Views

To create a list of regression models for predefined vectors, we can create a blank list and then use the for loop to create the list of regression models. For example, if we have two vectors say x and y, and we want to create a list of regression model between x and y then we create a blank list using list() and perform for loop a particular number of times as shown in the below examples.Example1 Live Demox

How to add approximately equal sign in a plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 11:22:11

589 Views

To add approximately equal sign in a plot using ggplot2, we can use tilde sign twice as ~~ in geom_text function of ggplot2 package. For example, we can do this by using the following syntax geom_text(aes(label="NULL%~~%")). Check out the below example to understand how it works.ExampleConsider the below data frame − Live Demox

Write a C program to demonstrate post increment and pre increment operators

Bhanu Priya
Updated on 06-Mar-2021 08:02:49

3K+ Views

Increment operator (++)It is used to increment the value of a variable by 1. There are two types of increment operators, pre-increment and post-increment.The increment operator is placed before the operand in pre-increment and the value is first incremented and then the operation is performed on it.For example, z = ++a; a= a+1 z=aThe increment operator is placed after the operand in post-increment and the value is incremented after the operation is performed.For example, z = a++; z=a a= a+1Example 1Following is an example for pre-increment operator − Live Demomain ( ){    int A= 10, Z;    Z= ++A;   ... Read More

Write C program using isupper() function

Bhanu Priya
Updated on 06-Mar-2021 07:59:15

213 Views

ProblemHow to identify a total number of upper-case alphabets in a string using C Programming?SolutionThe logic we used to count number of upper-case letters in a sentence is as follows −for(a=string[0];a!='\0';i++){    a=string[i];    if (isupper(a)){       counter=counter+1;       //counter++;    } }Example 1 Live Demo#include #include void main(){    //Declaring integer for number determination, string//    int i=0;    char a;    char string[50];    int counter=0;    //Reading User I/p//    printf("Enter the string :");    gets(string);    //Using For loop and predefined function to count upper case alpha's//    for(a=string[0];a!='\0';i++){       a=string[i]; ... Read More

Explain malloc function in C programming

Bhanu Priya
Updated on 06-Mar-2021 07:56:53

694 Views

ProblemWrite a C program to display and add the elements using dynamic memory allocation functions.SolutionIn C, the library function malloc allocates a block of memory in bytes at runtime. It returns a void pointer, which points to the base address of allocated memory and it leaves the memory uninitialized.Syntaxvoid *malloc (size in bytes)For example, int *ptr;ptr = (int * ) malloc (1000);int *ptr;ptr = (int * ) malloc (n * sizeof (int));Note − It returns NULL, if the memory is not free.Example Live Demo#include #include void main(){    //Declaring variables and pointers, sum//    int numofe, i, sum=0;    int *p; ... Read More

Write a C program using time.h library function

Bhanu Priya
Updated on 06-Mar-2021 16:33:29

353 Views

ProblemHow to display the current date and time in ISO standard format using C Programming language?SolutionThe current date and time of the input will be taken and we are trying to print the system time and date in ISO format.For example, Monday, Dec 15, 2020 10.50p.The built-in functions that we used in this program are −Time() − returns current time.Strftime() − converts the time to string form, this function include in time.h.Example Live Demo#include #include int main(){    time_t current = time(NULL);    char datetime[20];    strftime(datetime, sizeof(datetime), "%a, %d%b%y %H:%M", localtime(¤t));    puts(datetime);    return 0; }OutputThu, 31 Dec 20 ... Read More

Advertisements