1

apologies in advance I am a beginner to R. I have loaded a CVS file into a new data frame - One of the columns provides a category number (from 1 to 6).I want to create a loop to count the number of times each category number appears , and then store this within a new data frame. (The new data frame would be the category number and how many times it appears) I have created the below script so far however unsure how to store the results within the new data frame and include the category number.

Summarydf<-NULL

unique<-c(unique(Data$Type))

for (i in unique) {
Summarydf<-c(sum(Data$Type==i))

print(Summarydf)
}
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
Low
  • 29
  • 4
  • Hi, interesting, not sure if this might be of interest https://cmdlinetips.com/2018/02/how-to-get-frequency-counts-of-a-column-in-pandas-dataframe/ – IronMan Dec 03 '20 at 20:04
  • Thanks - ill have a read. I was hoping to complete with a loop/without the need to use an additional package function. – Low Dec 03 '20 at 20:07
  • Why not simply using the `table` function? – deschen Dec 03 '20 at 20:16
  • `as.data.frame(table(Data$Type))` seems right – Gregor Thomas Dec 03 '20 at 20:17
  • @IronMan Python tips for an R question? – Gregor Thomas Dec 03 '20 at 20:18
  • I agree the table function would be a lot easier however its a task i have to use a loop for unfortunately. The code brings back the correct result in the print() within the loop however cant work out how to store this to bind in a new dataframe – Low Dec 03 '20 at 20:25
  • Re: "however cant work out how to store this to bind in a new dataframe" If you must use a `for` loop you can initialize a vector outside the loop, `vec <- vector()` and augment the vector with each calculated value inside the loop, `vec <- c(vec, Summarydf)` `vec` will be your vector that you can bind to a dataframe. – SteveM Dec 03 '20 at 20:39
  • great thanks - that works! – Low Dec 03 '20 at 20:46

1 Answers1

1

You can just convert Data$Type to a factor and get a summary as a vector of the number of occurrences of each type. e.g.:

L <- LETTERS
Type <- sample(1:6, 26, replace = TRUE)
Data <- data.frame(L, Type)
Data$Type = as.factor(Data$Type)
summaryType <- summary(Data$Type)
summaryType
1 2 3 4 5 6 
4 4 5 7 3 3 
SteveM
  • 2,226
  • 3
  • 12
  • 16