So I have a .csv file with multiple rows and columns, and I want to extract the frequency of an occurrence for each column and input it into a table/data frame
I've done this but it gave me the occurrences for 1 column.
So I have a .csv file with multiple rows and columns, and I want to extract the frequency of an occurrence for each column and input it into a table/data frame
I've done this but it gave me the occurrences for 1 column.
Here is an example that could help.
Your data is not reproducible so kinda hard to know if this is what you want.
### Simulating dataframe
df <- data.frame(val1=sample(c("a", "b", "c"), 100, replace=TRUE),
val2=sample(c("Z", "Q", "R", "T"), 100, replace=TRUE))
### Computing number of occurences and detail by variable
summaryNbOccurences <- apply(df, 2, function(x) length(table(x)))
summaryTable <- apply(df, 2, function(x) table(x))
### Creating a table to summarize both information above
dfSummary <- data.frame(matrix(NA, nrow=ncol(df), ncol=3))
colnames(dfSummary) <- c("var", "nbOccurences", "table")
dfSummary$var <- colnames(df)
### Filling table
for(i in 1:ncol(df)){
dfSummary[i, "nbOccurences"] <- summaryNbOccurences[i]
dfSummary[i, "table"] <- paste(unlist(lapply(summaryTable, function(x) paste(names(x), ":", x, sep=""))[i]), collapse=" ")
}
Results
var nbOccurences table
1 val1 3 a:31 b:39 c:30
2 val2 4 Q:28 R:23 T:27 Z:22