R Programming Articles

Page 33 of 174

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 318 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 545 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 215 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

How to create a random vector of integers with increasing values only in R?

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

To create a random vector of integers with increasing values, we can do random sampling with sample.int and for increasing values cummax function needs to be used. For example, to create a random vector of integers of size 5 up to values 5 starting from 1 can be done by using the command cummax(sample.int(5)).Examplex1

Read More

How to find the frequency vector elements that exists in another vector in R?

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

If a vector value exists in another vector then we might want to find the frequency/count for such values in the other vector. For example, if we have two vectors say x and y, and some of the values in y exists in x as well. Therefore, we can find the frequency of values in x for y values can be found by using the command colSums(outer(x,y,"==")).Examplex1

Read More
Showing 321–330 of 1,740 articles
« Prev 1 31 32 33 34 35 174 Next »
Advertisements