Find total frequency of number In Column [duplicate] - r

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))

Related

Count occurrences of value in a set of variables in R (per column) [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 1 year ago.
I have this data and I want to figure out a way to know how many ones and how many zeros are in each column (ie Arts and Crafts). I have been trying different things but it hasn't been working. Does anyone have any suggestions?
You can use the table() function in R. This creates a categorical representation of your data. Additionally here convert list to vector I have used unlist() function.
df1 <- read.csv("Your_CSV_file_name_here.csv")
table(unlist(df1$ArtsAndCrafts))
If you want to row vice categorize the number of zeros and ones you can refer to this question in Stackoverflow.

Subsetting number of observations [duplicate]

This question already has answers here:
Remove last N rows in data frame with the arbitrary number of rows
(4 answers)
Closed 2 years ago.
I have a dataset consisting of 250 observations. I want to select all observations expect last. I know I can do this by following codes. But if do not know exact number of observations how I can do this.
dataset(mtcars)
mtcars_lag<-mtcars[1:31,]
## skipping first observation and selecting all
mtcars_forward<-mtcars[2:32,]
Using nrow() gets you the number of observations in the dataset. mtcars_subset <- mtcars[1:(nrow(mtcars)-1), ] will fetch you all observations except the last one.
EDIT: Added parenthesis in line with suggestion from MrFlick.

is there a way to set all cells in a dataframe in the form of a vector as NA? [duplicate]

This question already has answers here:
R: Count number of objects in list [closed]
(5 answers)
Closed 2 years ago.
I have a dataframe in R, and I am trying to set all cells in the form of a vector, either c(1,2,3) or 1:2 to NA. Is there any easy way to do this?
You can use lengths to count number of elements in each value of column. Set them to NA where the length is greater than 1. Here I am considering dataframe name as df and column name as col_name. Change them according to your data.
df$col_name[lengths(df$col_name) > 1] <- NA

sequential counts of a column based on unique identifier [duplicate]

This question already has answers here:
How do you find duplicated elements of a vector that outputs the integer appearance of that duplicate instead of a logical?
(1 answer)
Add column with order counts
(2 answers)
Closed 7 years ago.
I wish to create a new row of numbers (count) based on the number of occurrences of a unique identifier(unique_id) in the first column (using R).
For example:
unique_id <- c("1a","2b","2b","1c","1c","1c","3d","3d","1e","3b","3b","3b","3b")
count <- c(1,1,2,1,2,3,1,2,1,1,2,3,4)
data <- data.frame(unique_id,count)
I've had a good search, but because I'm unsure of the correct terminology for this (still new to R), I was unable to find any relevant info.

How to take a data.frame and create a table of only the Max values? [duplicate]

This question already has answers here:
How to find the highest value of a column in a data frame in R?
(10 answers)
Closed 7 years ago.
I have the following dataframe:
I would like to create a table of the column headers, in a column with their maximum value to the right of them. I will be looking to embed this in a Shiny App. Does anyone know how I can do this?
I used:
colMax <- function(data) sapply(data, max, na.rm=TRUE)
Then I called as.data.frame(colmax(data)) and it worked as needed.
Duplicate: How to find the highest value of a column in a data frame in R?

Resources