how do i round numbers in a column in R? [duplicate] - r

This question already has answers here:
Formatting Decimal places in R
(15 answers)
Closed 2 years ago.
How do I round the numbers to 2 decimal points within a column within a dataframe?
The name of the df is tax_data and the column that I want to round is called rate_percent
I tried using:
format(round(rate_percent ,2), nsmall =2) but this didn't work.
Does anyone have any suggestions?

Here, in Base-R
tax_data$rate_percent <- round(tax_data$rate_percent, 2)

Related

How to extract number from string? [duplicate]

This question already has answers here:
stringr extract full number from string
(1 answer)
Extracting unique numbers from string in R
(7 answers)
Extracting numbers from vectors of strings
(12 answers)
Closed 3 years ago.
I have
strQuestions <- c("Q1", "Q22")
I need to get
nQuestions <- c(1,22)
How can I do it nicely? (with stringr?). Thank you
With stringr:
str_sub(strQuestions, 2, 3)
stringr::str_remove_all(strQuestions,"[A-Za-z]")
You can add the function as.numeric() after.

Repeating entries in match in R [duplicate]

This question already has answers here:
Find all positions of all matches of one vector of values in second vector
(3 answers)
Finding All Positions for Multiple Elements in a Vector
(7 answers)
Closed 4 years ago.
In the following example, there is repeating entries in the second vector. How do I make R to tell me the position of both appearances of 5, please?
match(5, c(1,2,9,5,3,6,7,4,5))
Use the which function:
which(c(1,2,9,5,3,6,7,4,5) == 5)

Find total frequency of number In Column [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 5 years ago.
I am new on R. I want to ask, How to find frequency of each Number in Column, there are multiple numbers in column. i want to frequency of each number. I want just simple code. You can imagine that data set name is Oct-TT. Thanks
Here is the answer:
df <- as.data.frame(sample(10:20, 20,replace=T))
colnames(df) <- "Numbers"
View(df)
as.data.frame(table(df$Numbers))

How do i take the last n elements from a vector in R? [duplicate]

This question already has answers here:
Getting the last n elements of a vector. Is there a better way than using the length() function?
(6 answers)
Closed 5 years ago.
suppose I have daily time series data under variable name "prices", but im only interested in the past 100 days. How would i extract the last 100 elements from this variable?
Something equivalent to python's prices[-100:] but for R?
If it's a vector:
tail(prices, 100)

change numbers into the sum of their figures [duplicate]

This question already has answers here:
Digit sum function in R
(4 answers)
Closed 7 years ago.
I've got this simple question: how can I change a vector consisting of 10 numbers into a vector consisting of ten numbers which are the sum of the figures of the first numbers? So 11 in the first vector becomes 2, 234 becomes 9.
We can use str_extract_all from stringr to get the individual numbers, convert them to numeric and get the sum.
library(stringr)
sapply(str_extract_all(c(11, 234), '\\d'), function(x) sum(as.numeric(x)))

Resources