Value entered twice in one vector [closed] - r

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a column with a range 1:5. Many vectors contain the value twice, for example (33) instead of (3) and (11) instead of 1. Is there any code to delete these duplicates and keep one number within a vector?
Thanks a lot

Say your column is named y:
substr(y,1,1)
Takes from the first digit to the first digit (i.e. only the first) of y (element-wise). But substr outputs a character, to transform it back to numeric:
as.numeric(substr(y,1,1))

Related

Showing all rows where two columns average is greater than 60 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
How would I show (display) all rows of a data set whose average of columns 1 and columns 2 is great than 60
Assuming that you mean the average by row, and that the data frame is named yourdata, something like this should work:
yourdata[rowMeans(yourdata[1:2]) > 60, ]

Can I use Grep or Grepl to return instances of two capital letters together in text? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm interested to pull out instances where initials are used in my text data, as they are almost always two capital letters, is there a simple way to look for this text pattern using Grep in R?
If we need to match two upper case letters from the start (^) of the string, use grepl on the column concerned to return a logical expression within subset to subset the rows
subset(df1, grepl("^[A-Z]{2}", col1))

Advanced subsetting r data frame [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Without using a special function, can you do the following to change the values in an R dataframe from a column that have length greater than 4:
df[length(df$Column1)>4,"Column1"] = "replacement value"
This does not seem to work, is there an alternative index style I can use, or do I need to use a function?
Thanks
The function to determine the length of an entry, like a word in a dataframe, is nchar(), and not length(). The latter is typically used to determine the number of entries in a vector.
You could therefore try using:
df[nchar(df$Column1) > 4, "Column1"] <- "replacement value"

Maximum factorial that is formed by three digits? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Number of digits that are present in the maximum number that is formed using three digits?
Maximum factorial that is formed by three digits?
This was a question asked on a site.
I am not able to understand is there any thing tricky i am not getting?
i have tried 3 and 720 but it is incorrect
The maximum factorial which can be formed using 3 digits is 999!.
The answer can be easily obtained from wolfram alpha.
Number of digits in 999!.
999!=Answer

Why does data get altered while applying a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I loaded a RDS file. The file contains a numeric field. When I say
class(NEI$Emissions)
it returns
"numeric"
The data is in maximum 3 digits and contains 3 digits of decimal. However, when I issue the command
max(NEI$Emissions)
it returns a huge number.
646952
How can I use the numeric values as it is?
R doesn't lie. One of your data points is not what you expect.
Find which row has the problem with this command:
which.max(NEI$Emissions)
then examine that row of your original data. You will find the errant value.

Resources