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.
This question already has answers here:
Numbering rows within groups in a data frame
(10 answers)
Create counter with multiple variables [duplicate]
(6 answers)
generate sequence within group in R [duplicate]
(1 answer)
Closed 1 year ago.
I really need your help and I'm sorry in advance for my english !
I have to add a counter variable to my dataframe. A first grouping is made with the ID and num_order variables. In this grouping my counter should number the rows as a rank but assign the same rank to similar second grouping (col1, col2, ok).
This is my dataframe :
df <-
data.frame(id=c("A03","A03","A03","A03","A03","A03","A03","A03","A03","A03","A03","Z07","Z07","Z07"),
num_order=c("17","17","17","17","17","17","17","17","5","5","5","23","23","23"),
col1=c("aaa","aaa","bbb","ccc","ccc","ddd","eee","ccc","aaa","hhh","ccc","ddd","eee","eee"),
col2=c("aaa","aaa","bbb","ccc","ccc","ddd","eee","eee","aaa","hhh","ccc","ddd","eee","eee"),
ok=c("yes","yes","no","no","no","no","no","no","yes","no","no","no","no","no"),
counter=c(1,1,2,3,3,4,5,6,1,2,3,1,2,2))
The result I am looking for is in the counter column.
Thanks a lot for your help
This question already has answers here:
Mean per group in a data.frame [duplicate]
(8 answers)
How to find mean for subset using R?
(2 answers)
Closed 3 years ago.
Beginner problem in R. I have a data frame in R that has 1 column ranking the data into specific subgroups, named "Group" (Groups: Heavy, Medium, Light), then another column with values (named "Value") of those groups. How do I check, for example the mean, within the "value" column for a specific subset based on the "Group" column, lets say for "Heavy"?
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))
This question already has answers here:
R: Removing duplicate elements in a vector [duplicate]
(4 answers)
Closed 6 years ago.
I'm trying to use R to delete all the duplicate values without keeping the original value.
For example I've the following list
1. Car
2. Car
3. Food
Unique function will remove the the duplicate and will keep the original.
1. Car
2. Food
I need to to remove all the duplicate values and to get only the value that occurred once.
1. Food
Thank you in advance
We can use
df1[!(duplicated(df1$col)|duplicated(df1$col, fromLast=TRUE)),, drop=FALSE]
# col
#3. Food