1

I am having trouble referencing columns in a dataframe by name. The function i have begins with extracting rows where no NA's are present:

prepare <- function(dataframe, attr1,attr2){
subset_na_still_there <- dataframe[!is.na(attr1) & !is.na(attr2),]
subset_na_still_there2 <- subset(dataframe, !is.na(attr1) & !is.na(attr2))
### someother code goes here
}

However, the subsets that are returned still contain NA's. I get no errors. Here is a related question

edit: Selecting the columns and then referencing them by number does the trick:

prepare <- function(dataframe, attr1,attr2){
subset_cols <- dataframe[,c(attr1, attr2)]
subset_gone <- subset_cols[!is.na(subset_cols[,1]) & !is.na(subset_cols[,2]),]
}

Why does the first version not work as expected?

Community
  • 1
  • 1
Ivana
  • 643
  • 10
  • 27

3 Answers3

3

How about this:

prepare <- function(x, attr1, attr2){
  x[!is.na(x[attr1]) & !is.na(x[attr2]),]
}
seancarmody
  • 6,182
  • 2
  • 34
  • 31
1

Rather than creating your own function, try subset:

subset(mydata, !is.na(attr1) & !is.na(attr2))

If you want to get rid of rows with NAs in any field try

na.omit(mydata)
seancarmody
  • 6,182
  • 2
  • 34
  • 31
  • I'm using the subset function in the second line, the problem is not with is.na() but with the column reference. – Ivana Aug 17 '12 at 10:02
1
df <- data.frame(att1=c(1,NA,NA,10),att2=c(NA,1,2,3),val=c("a","z","e","r"))

df
  att1 att2 val
1    1   NA   a
2   NA    1   z
3   NA    2   e
4   10    3   r

test <- function(df,att1,att2){
df_no_na <- df[!is.na(att1) & !is.na(att2),]
df_no_na
}

test(df,df$att1,df$att2)
  att1 att2 val
4   10    3   r

It's work for me. Are you sure about NA's ? Is is.na(df$att1) return TRUE ?

Alan
  • 3,153
  • 2
  • 15
  • 11
  • I suspect that the OP may have been trying to call the function as `prepare(mydf, "att1", "att2")`. Just a guess! – seancarmody Aug 17 '12 at 10:02
  • Yes, i confess. But i get a "object 'att2' not found" if i dont. I expect to be passing a name, not a column. – Ivana Aug 17 '12 at 10:09
  • As a sidenote, if i were to pass a reference the attribute and not the name of the attribute, is there any name to obtain the name inside the function? plotHist <- function(attribute) {hist(attribute, main=cat(attribute))} – Ivana Sep 10 '12 at 11:42