How is my data stored in R? -


so, i'm trying figure out larger problem, , think may stem what's happening when import data .txt file. regular beginning commands are:

data<-read.table("mydata.txt",header=t) attach(data) 

so if data has say, 3 columns headers "var1", "var2" , "var3", how imported? seems though imported 3 separate vectors, bound together, similar using cbind().

my larger issue modifying data. if row in data frame has empty spot (in column) need remove it:

data <- data[complete.cases(data),] 

perfect - original data frame had 100 rows, 5 of had empty slot. new data frame should have 95 rows, right? if try:

> length(var1) [1] 100 > length(data$var1) [1] 95 

so seems original column labelled var1 unaffected line rewrote entire data frame. why believe when import data, have 3 separate columns stored somewhere called var1, var2 , var3. far getting r recognize want modified version of column, think need along lines of:

var1 <- data$var1 #repeat every variable 

my issue need write above bit of code every single variable. data frame have large, , way of coding seems tedious. there better way me transform data, able call modified variables, without needing use data$ precursor every time?

read.table() reads data data frame component (column) each column (variable) in text file. r's data frame excel spreadsheet, each column in sheet can contain different type of data (contrast matrix, in r can contain data of single type).

in effect, result if data read in column column , bound column-wise using cbind.data.frame() method. not how done in practice though. have single object data 3 components, none of can accessed typing name (e.g. var1). try exactly this

data <- read.table("mydata.txt", header = true) var1 

in clean session (best if start new session try this, in case).

if type ls() see data listed (assuming clean session). clearl evidence against thinking have 3 columns , individual objects.

the real problem here attach() not read.table().

there few uses of attach() , 1 show not among them. attach(data) places copy of data on search path. key point there copy. on search path not same thing data in global environment (your workspace). changes data in global environment not reflected in copy on search path, because these two, separate objects.

r has search path looks named objects. r doesn't inside objects , hence var1 etc not found whenever type name @ prompt or attempt use object directly. when attach() object can think of opening object r's search. thing catches people out 1 looking inside copy of object , not object itself.

in interactive sessions, there useful helper functions mean don't need typing data$ time. see ?with, ?within, ?transform example.

really don't use attach() in lieu of bit of typing.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -