I’m new to R and I’m trying to create a .R script that will open up a .csv file of mine and compute some frequencies. There are headers in this file and the values associated with them are either 1,0,NA, or -4. What I want to do is go through each vertical row and then compute the frequencies of them. I’m sure this is an easy script, but I’m not sure how the syntax of R works yet. Can anyone get me started on this please?
Share
The exact script is going to vary based on your input and what kind of output you’d like (just printed to the interactive console? Written to .csv?), but here’s my attempt:
The
applyfunction applies the function you give it (FUN) over the margin (1 = rows, 2 = columns) of the data that you give it. You can give it any function you like. PassingFUN = summarywill give you the mean, min, max, etc. of each column (because they’re numeric). But the default method of summary() for factors is frequencies, which is what you need. So instead of passing summary, trick R into seeing your numbers as a factor: define an anonymous functionfunction(x)(apply will know that by x you’re referring to the columns taken one at a time). Set this function to first convert x to a factor (factor(x)) and then summarize that factor. This will return a matrix with the frequencies for each column.Not the most elegant code ever, but I think it’ll get you what you need.