Proc stands for basic SAS for R

2

Hello, I am a basic user of SAS and need to create a table in R, but I am well lost in R.

The command given in SAS is

DATA MEDIAS; SET TEMP;
PROC MEANS N MEAN MIN MAX STD;
VAR a1  a2  b1  b2  c1  c2  T R E;
RUN;

In sas it frees me this table:

So far in R I have been able to insert the data as a data frame with the variables, but I need it to organize these variables in column, and drop the number of observations of each, the averages, the maximums the minimums and the standard deviation. I have lost data in this file that is already in NA format.

Can anyone help me in the code in R?

    
asked by anonymous 02.02.2016 / 23:48

2 answers

2

The summary function already gives you enough, at least, first quartile, median, average, third quartile and maximum for each variable. For example:

summary(mtcars)
      mpg             cyl             disp             hp             drat             wt       
 Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0   Min.   :2.760   Min.   :1.513  
 1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5   1st Qu.:3.080   1st Qu.:2.581  
 Median :19.20   Median :6.000   Median :196.3   Median :123.0   Median :3.695   Median :3.325  
 Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7   Mean   :3.597   Mean   :3.217  
 3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0   3rd Qu.:3.920   3rd Qu.:3.610  
 Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0   Max.   :4.930   Max.   :5.424  
      qsec             vs               am              gear            carb      
 Min.   :14.50   Min.   :0.0000   Min.   :0.0000   Min.   :3.000   Min.   :1.000  
 1st Qu.:16.89   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
 Median :17.71   Median :0.0000   Median :0.0000   Median :4.000   Median :2.000  
 Mean   :17.85   Mean   :0.4375   Mean   :0.4062   Mean   :3.688   Mean   :2.812  
 3rd Qu.:18.90   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
 Max.   :22.90   Max.   :1.0000   Max.   :1.0000   Max.   :5.000   Max.   :8.000

You can also apply your own functions in data.frame using sapply , for example:

sumario <- function(x) c(Nobs = length(x), Media = mean(x), Min = min(x), Max = max(x), SD = sd(x))
t(sapply(mtcars, sumario))
     Nobs      Media    Min     Max          SD
mpg    32  20.090625 10.400  33.900   6.0269481
cyl    32   6.187500  4.000   8.000   1.7859216
disp   32 230.721875 71.100 472.000 123.9386938
hp     32 146.687500 52.000 335.000  68.5628685
drat   32   3.596563  2.760   4.930   0.5346787
wt     32   3.217250  1.513   5.424   0.9784574
qsec   32  17.848750 14.500  22.900   1.7869432
vs     32   0.437500  0.000   1.000   0.5040161
am     32   0.406250  0.000   1.000   0.4989909
gear   32   3.687500  3.000   5.000   0.7378041
carb   32   2.812500  1.000   8.000   1.6152000
    
03.02.2016 / 18:04
1

You will be able to use the code below:

Replace mtcars with your database. Also make sure that all variables in your database are numeric, otherwise the code will fail.

library(plyr)
library(tidyr)
d <- ldply(mtcars, function(x){
  data.frame(
    nobs = sum(!is.na(x)),
    mean = mean(x, na.rm = T),
    min = min(x, na.rm = T),
    max = max(x, na.rm = T),
    sd = sd(x, na.rm = T)
  ) 
}) %>% 
  gather(medida, valor, - .id) %>%
  spread(.id, valor)

If you need to install plyr or tidyr use install.packages("nome_pacote") .

  medida         am    carb       cyl     disp       drat       gear        hp       mpg
1   nobs 32.0000000 32.0000 32.000000  32.0000 32.0000000 32.0000000  32.00000 32.000000
2   mean  0.4062500  2.8125  6.187500 230.7219  3.5965625  3.6875000 146.68750 20.090625
3    min  0.0000000  1.0000  4.000000  71.1000  2.7600000  3.0000000  52.00000 10.400000
4    max  1.0000000  8.0000  8.000000 472.0000  4.9300000  5.0000000 335.00000 33.900000
5     sd  0.4989909  1.6152  1.785922 123.9387  0.5346787  0.7378041  68.56287  6.026948
       qsec         vs         wt
1 32.000000 32.0000000 32.0000000
2 17.848750  0.4375000  3.2172500
3 14.500000  0.0000000  1.5130000
4 22.900000  1.0000000  5.4240000
5  1.786943  0.5040161  0.9784574
    
03.02.2016 / 14:43