-3

code snippet

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.

SternK
  • 11,649
  • 22
  • 32
  • 46
N Tmz
  • 1
  • 1
  • "I've done this" is quite unspecific. Please share a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a small example of your data used. – Martin Gal Nov 25 '22 at 10:18
  • My apologies, it did not include the image mutation<- read.csv("mutation.csv", stringsAsFactors = FALSE, row.names = 1) mutation table(mutation$pik3ca_mut) freq<-length(which(mutation$pik3ca_mut!=0)) freq But this gives me for 1 column and I'm trying to find for multiple columns separately – N Tmz Nov 25 '22 at 10:25
  • Please edit your question and put the code into it. – Martin Gal Nov 25 '22 at 10:44

1 Answers1

0

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
Yacine Hajji
  • 1,124
  • 1
  • 3
  • 20