Server Side Programming Articles

Page 1133 of 2109

How to remove the first and last character in a string in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 11K+ Views

To remove the first and last character in a string, we can use str_sub function of stringr package. For example, if a word say tutorialspoint is mistakenly typed as ttutorialspointt and stored in a vector called x then to remove the first and last “t”, we can use the command str_sub(x,2,−2).Example1library(stringr) x1

Read More

How to change the name of variables in a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

The name of variables in a list are actually the list elements. These elements can be either named or unnamed. The naming can be done with the help of names function and renaming can be done in the same way as well. For example, if we have a list called LIST then the names of the element in LIST can be done by using the below command: names(LIST)

Read More

How to multiply all values in a list by a number in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 4K+ Views

To multiply all values in a list by a number, we can use lapply function. Inside the lapply function we would need to supply multiplication sign that is * with the list name and the number by which we want to multiple all the list values. For example, if we have a list called LIST and we want to multiply each value in LIST by 2 then it can be done by using the command lapply(LIST,"*",2).Example1List1

Read More

How to perform rounding in R to next 10 instead of nearest 10?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 317 Views

In general, most commonly used rounding is rounding to nearest 10 or nearest 100 but sometimes we actually want to remove the values after a value instead of rounding. For example, removing values after 2 decimal places, this is the type of situation where we need to round to next 10 instead of nearest 10. This can be done with the help of floor function as shown in the below examples.Example1x1

Read More

How to remove spaces at the end in string vectors in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 543 Views

Sometimes values in a string vector has an extra space at the end, this might happen while typing the values or due to some other manual errors. To remove spaces at the end in string vectors, we can use gsub function. For example, if we have a vector called x that contains string values with spaces at the end then the removal of values can be done by using the command gsub(" $","",x,perl=T)Examplex1

Read More

How to find the rate of return for a vector values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 214 Views

To find the rate of return for a vector values, we can use the formula for rate of return. For example, if we have a vector called x then the rate of return can be calculated by using the syntax diff(x)/x[-length(x)]. The output will be in decimal form and if we want to convert it to percentage then the output needs to be multiplied with 100, we can also input the same within the formula as (diff(x)/x[-length(x)])*100.Examplex1

Read More

How to remove dot and number at the end of the string in an R vector?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 7K+ Views

To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space. After that the vector will be passed as shown in the below examples.Example1x1

Read More

How to sort a large number of csv files in ascending order in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 359 Views

To sort a large number of csv files in ascending order, we can use mixedsort function from gtools package. For example, if we have a list of csv files that are randomly arranged in a vector called FILES then the files can be sorted in ascending order using the command mixedsort(sort(FILES))ExampleFiles1

Read More

How to truncate a string vector after a character in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

The most difficult problem in data analysis is cleaning a dirty data. Most of the times the data is available in dirty form and one such dirtiness is a string vector having unnecessary values after a particular character. Therefore, to truncate a string vector after a character we can use str_split from stringr package along with the sapply function as shown in the below examples.library(stringr)Examplex1

Read More

How to create reverse of a number in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To create reverse of a number, we can use stri_reverse function of stringi package. For example, if we have a vector called x that contain some numbers then the reverse of these numbers will be generated by using the command stri_reverse(x). But the output will be in character form, if we want to have numeric values then as.numeric function can be used.library(stringi)Examplex1

Read More
Showing 11321–11330 of 21,090 articles
Advertisements