Found 33676 Articles for Programming

How to create a random sample of some percentage of rows for a particular value of a column from an R data frame?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:10:04

941 Views

Random sampling is an important part of data analysis, mostly we need to create a random sample based on rows instead of columns because rows represent the cases. To create a random sample of some percentage of rows for a particular value of a column from an R data frame we can use sample function with which function.Consider the below data frame −Example Live Demoset.seed(887) grp

How to create a barplot with one of the bars having different color in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:06:57

558 Views

A bar plot represents discrete data and the bars in the bar plot are usually of same color but we might want to highlight a particular bar based on the characteristics of the data or the objective of the analysis project. For example, if a particular bar represents highly severe situation or highly unimportant situation then we can change the color that particular bar so that people can easily point out that bar.Consider the below data frame −Example Live Demox

How to add a string before each numeric value in an R data frame column?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:03:10

4K+ Views

Sometimes the unique identity column of a data frame is not recorded as intended, it contains only numeric values that does not solve the data characteristic purpose. Therefore, we might want to add a string before those numeric values to make the data more sensible for viewers and analysts. This can be easily done with the help of gsub function.Consider the below data frame −Example Live Demoset.seed(111) x1

The easiest way to concatenate two arrays in PHP?

AmitDiwan
Updated on 13-Oct-2020 08:50:40

3K+ Views

Simply use, array_merge() to concatenate two arrays in PHP. Let’s say the following are out arrays −$nameArray1 = array('John','David'); $nameArray2 = array('Mike','Sam');Now, set both the above arrays in array_merge() to concatenate them.The syntax is as follows −array_merge($yourFirstArrayName, $yourSecondArrayName);ExampleThe PHP code is as follows  Live Demo OutputThis will produce the following outputArray ( [0] => John [1] => David [2] => Mike [3] => Sam )

PHP make sure string has no whitespace?

AmitDiwan
Updated on 13-Oct-2020 08:48:42

3K+ Views

To check whether a string has no whitespace, use the preg_match() in PHP.The syntax is as follows preg_match('/\s/',$yourVariableName);ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe name (John Smith) has the space

How to add time in minutes in datetime string PHP?

AmitDiwan
Updated on 13-Oct-2020 08:46:42

11K+ Views

For this, you can use the strtotime() method.The syntax is as follows −$anyVariableName= strtotime('anyDateValue + X minute');You can put the integer value in place of X.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following output2020-10-30 10:15:20

How to count values from a PHP array and show value only once in a foreach loop?

AmitDiwan
Updated on 13-Oct-2020 08:44:41

1K+ Views

Let’s say the following is our PHP array $listOfNames = array('John','David','Mike','David','Mike','David');We want the output to display the count of values in the above array like this −Array ( [John] => 1 [David] => 3 [Mike] => 2 )To get the count, use inbuilt function array_count_values().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [John] => 1 [David] => 3 [Mike] => 2 )

How to remove a character (‘’) in string array and display result in one string php?

AmitDiwan
Updated on 13-Oct-2020 08:41:18

322 Views

Let’s say the following is our string array −$full_name= '["John Doe","David Miller","Adam Smith"]';We want the output in a single string −John Doe, David Miller, Adam SmithFor this, use json_decode().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputThe Result in one string=John Doe, David Miller, Adam Smith

Instance child class in abstract static method PHP?

AmitDiwan
Updated on 13-Oct-2020 08:40:06

549 Views

For this, use $anyObjectName=new static() along with self.ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputJohn Doe

Generate array of random unique numbers in PHP?

AmitDiwan
Updated on 13-Oct-2020 08:37:21

945 Views

For random unique numbers array, use range() along with shuffle().ExampleThe PHP code is as follows Live Demo OutputThis will produce the following outputArray ( [0] => 11 [1] => 14 [2] => 15 [3] => 12 [4] => 18 )

Advertisements