Wednesday, March 23, 2011

http://www.ats.ucla.edu/stat/R/notes/entering.htm

> data1 <- read.table("http://www.ats.ucla.edu/stat/R/notes/hs0.csv", header=T, sep=",")
> names(data1)
 [1] "gender"  "id"      "race"    "ses"     "schtyp"  "prgtype" "read"    "write"   "math"    "science" "socst"
> head(data1)
  gender  id race ses schtyp  prgtype read write math science socst
1      0  70    4   1      1  general   57    52   41      47    57
2      1 121    4   2      1   vocati   68    59   53      63    61
3      0  86    4   3      1  general   44    33   54      58    31
4      0 141    4   3      1   vocati   63    44   47      53    56
5      0 172    4   2      1 academic   47    52   57      53    61
6      0 113    4   2      1 academic   44    52   51      63    61
> dim(data1)
[1] 200  11
> # saves as an R object
> save(data1,file="data1.rda")
> # checking to see if data1.rda has been created
> dir()
 [1] "asg1-40"                           "data1.rda"                         "desktop.ini"                       "FMS_data.txt"                      "FMS_data_edited.dat"            
 [6] "FMS_data_edited.sav"               "HGDP_AKT1.txt"                     "hs0.rda"                           "JHS_NPHP1_single_sheet_wy0111.dat" "MolecularWeight_tair7.xls"      
[11] "My Music"                          "My Pictures"                       "My Videos"                         "R"                                 "rtn.txt"                        
[16] "rtn.xlsx"                          "SafeNet Sentinel"                  "SPSSInc"                           "TargetP_analysis_tair7.xls"        "test1230.txt"                    
[21] "Virco_data.csv"                  
# list of rda files
> ls() 
[1] "data1" "hs0"
# clear everything out of memory
> rm(list=ls()) 
> ls()
character(0)
> # load the R data into memory
> load("data1.rda")
> tail(data1)
    gender  id race ses schtyp  prgtype read write math science socst
195      1 179    4   2      2 academic   47    65   60      50    56
196      1  31    2   2      2  general   55    59   52      42    56
197      1 145    4   2      1   vocati   42    46   38      36    46
198      1 187    4   2      2  general   57    41   57      55    52
199      1 118    4   2      1  general   55    62   58      58    61
200      1 137    4   3      1 academic   63    65   65      53    61
>
> rm(list=ls())  # clear everything out of memory
> ls()
character(0)


> temp <- read.table("http://www.ats.ucla.edu/stat/R/notes/hs0_1.csv", header=F, sep=",")
> temp[9,]
  V1 V2 V3 V4 V5      V6 V7 V8 V9 V10 V11
9  0 84  4  2  1 general 63 57 54  NA  51
> names(temp) <- c("gender","id","race","ses","schtyp","prgtype","read","write","math","science","socst")
> # list observations 5 through 10 to check the data
> temp[5:10, ]
   gender  id race ses schtyp  prgtype read write math science socst
5       0 172    4   2      1 academic   47    52   57      53    61
6       0 113    4   2      1 academic   44    52   51      63    61
7       0  50    3   2      1  general   50    59   42      53    61
8       0  11    1   2      1 academic   34    46   45      39    36
9       0  84    4   2      1  general   63    57   54      NA    51
10      0  48    3   2      1 academic   57    55   52      50    51
>



No comments:

Post a Comment