Programming Articles - Page 1369 of 3366

How to split month and year from 6-digit numbers in an R data frame column?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 20:05:07

486 Views

Sometimes we get data that is not in the form to proceed with the analysis and one such situation is dates stored in 6-digit numbers as 202105 that represents fifth month of year 2021 instead of date format as 2021/05. Therefore, we need to split the date and extract the month and year from the number. This can be done easily with the help of transform function as shown in the below examples.Example1Consider the below data frame −Live Demo> Date Response1 df1 df1Output   Date    Response1 1 202103   0.946367628 2 202103   1.241718518 3 202101  -0.657920816 4 202103  -0.809622853 ... Read More

How to find the row and column index of a character value in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 20:19:52

5K+ Views

To find the row and column index for a numerical value in an R data frame we use which function and if the value is character then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say Data then we can find the row and column index of Data by using the command as which(df=="Data", arr.ind=TRUE).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output    x1    x2 1 Female  5 2 Female  5 3 Female  6 4 Female ... Read More

C Program to find minimum occurrence of character in a string

Bhanu Priya
Updated on 24-Mar-2021 14:12:33

2K+ Views

An array of characters is called a string.DeclarationFollowing is the declaration for declaring an array is as follows −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.Finding minimum occurrenceThe logic to find minimum occurrence of a character in a given string is as follows −for(i=0; i

C program to print array of pointers to strings and their address

Bhanu Priya
Updated on 19-Mar-2021 10:04:52

8K+ Views

First, let us understand what are the arrays of pointers in C programming language.Arrays of pointers: (to strings)It is an array whose elements are ptrs to the base add of the string.It is declared and initialized as follows −char *a[ ] = {"one", "two", "three"};Here, a[0] is a pointer to the base add of string "one".         a[1] is a pointer to the base add of string "two".         a[2] is a pointer to the base add of string "three".AdvantagesThe advantages of array of pointers are explained below −Unlink the two dimensional array of characters, in ... Read More

What is strrev() Function in C language?

Bhanu Priya
Updated on 19-Mar-2021 10:01:46

4K+ Views

An array of characters is called a string.DeclarationThe syntax for declaring an array is as follows −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string "%s" used for accessing the string till it encounters ‘\0’.The strrev( ) FunctionThis function is used for reversing a string.The reversed string is stored in the same string.SyntaxThe syntax for strrev() function is as follows −strrev (string)ExampleThe following program shows the usage of strrev() function.#include main ( ... Read More

What is strncpy() Function in C language?

Bhanu Priya
Updated on 19-Mar-2021 09:50:54

765 Views

The C library function char *strncpy(char *dest, const char *src, size_t n) copies up to n characters from the string pointed to, by src to dest. In a case where, the length of src is less than that of n, the remainder of dest will be padded with null bytes.An array of characters is called a string.DeclarationFollowing is the declaration for an array −char stringname [size];For example − char string[50]; string of length 50 charactersInitializationUsing single character constant −char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}Using string constants −char string[10] = "Hello":;Accessing − There is a control string ... Read More

Explain pointers and two-dimensional array in C language

Bhanu Priya
Updated on 17-Mar-2021 10:23:57

7K+ Views

Pointer is a variable that stores the address of another variable.FeaturesPointer saves the memory space.Execution time of pointer is faster because of direct access to memory location.With the help of pointers, the memory is accessed efficiently, i.e., memory is allocated and deallocated dynamically.Pointers are used with data structures.Pointers and two dimensional arraysMemory allocation for a two-dimensional array is as follows −int a[3] [3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};a[1] [2] = *(1234 + 1*3+2) = *(1234 + 3+2) = *(1234 + 5*4) // 4 is Scale factor = * (1234+20) = *(1254) a[1] [2] = 6ExampleFollowing ... Read More

How to send an entire array as an argument in C language?

Bhanu Priya
Updated on 17-Mar-2021 10:00:40

303 Views

An array is a group of related items that is stored with a common name.Declaring arrayThe syntax for declaring an array is as follows −datatype array_name [size];InitializationAn array can be initialized in two ways, which are as follows −Compile time initialization.Runtime initialization.An array can also be initialized at the time of declaration as follows −int a[5] = {100, 200, 300, 400, 500};FunctionA function is a self-contained block that carries out a specific well-defined task. The two ways of passing the arrays as arguments to functions are as follows −Sending an entire array as an argument to function.Sending the individual elements ... Read More

How to find the unique rows in an R data frame?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 20:44:08

1K+ Views

A unique row in an R data frame means that all the elements in that row are not repeated with the same combination in the whole data frame. In simple words, we can say that if we have a data frame called df that contains 3 columns and 5 rows then all the values in a particular row are not repeated for any other row. The search of this type of rows might be required when we have a lot of duplicate rows in our data set. To do this, we can use group_by_all function of dplyr package as shown ... Read More

How to merge two matrices by combining rows in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 20:55:21

463 Views

By combining rows means that we want to concatenate rows of matrices but create separate columns as in the original matrices. For example, if we have two matrices say M1 and M2 as shown below −M1 1 2 3 3 2 1 M2 2 3 5 1 2 3Then merging of these two matrices by combining rows will result in −1 2 3 2 3 5 3 2 1 1 2 3Example1Live Demo> M1 M1Output      [, 1] [, 2] [1, ]  5     2 [2, ]  7     4 [3, ]  3     6 ... Read More

Advertisements