[R] Trouble with simple R list concatenations
jim holtman
jholtman at gmail.com
Mon Mar 21 02:58:52 CET 2011
First of all, you are working with 'vectors' not 'lists'. Look in the
following script what str(family) is on your original input.
What you probably want is a 'data.frame'. May be time to go back and
re-read the "Intro to R" to understand the differences between
vectors, lists and dataframes. You may also want to understand what
'factors' are since by default that is what character strings are
converted to in dataframes, and that may not be what you want.
> Birth_Date <- NULL
> Birth_Date[1:3] <- c("01/17/1939","01/17/1949", "01/17/1959")
> Later_Date <- NULL
> Later_Date[1:3] <- c("01/17/2009", NA, NA)
> Names <- NULL
> Names[1:3] <- c("Martha Smith", "John Doe", "Rufus Nobody")
>
> #this does not work
> family <- c(Birth_Date, Later_Date, Names)
> family$Birth_Date
Error in family$Birth_Date : $ operator is invalid for atomic vectors
No suitable frames for recover()
> str(family) # notice that this is a character vector of size 9
chr [1:9] "01/17/1939" "01/17/1949" "01/17/1959" "01/17/2009" NA NA
"Martha Smith" ...
> family <- data.frame(Birth_Date, Later_Date, Names)
> str(family)
'data.frame': 3 obs. of 3 variables:
$ Birth_Date: Factor w/ 3 levels "01/17/1939","01/17/1949",..: 1 2 3
$ Later_Date: Factor w/ 1 level "01/17/2009": 1 NA NA
$ Names : Factor w/ 3 levels "John Doe","Martha Smith",..: 2 1 3
> family$Birth_Date
[1] 01/17/1939 01/17/1949 01/17/1959
Levels: 01/17/1939 01/17/1949 01/17/1959
>
On Sun, Mar 20, 2011 at 9:32 PM, Jim Burke <j.burke at earthlink.net> wrote:
> PROBLEM How can I concatenate the following lists into ONE LIST WITHOUT the
> unhelpful message "operator is invalid for atomic vectors"? Combine as a
> data frame?
>
> EXAMPLE
>
> Birth_Date <- NULL
> Birth_Date[1:3] <- c("01/17/1939","01/17/1949", "01/17/1959")
> Later_Date <- NULL
> Later_Date[1:3] <- c("01/17/2009", NA, NA)
> Names <- NULL
> Names[1:3] <- c("Martha Smith", "John Doe", "Rufus Nobody")
>
> #this does not work
> family <- c(Birth_Date, Later_Date, Names)
> family$Birth_Date
> Error in family$Birth_Date : $ operator is invalid for atomic vectors
>
> FUNCTIONALLY DESIRED FROM THE FINAL COMBINED data frame
> I would appreciate being able to do the following with the final list or
> data frame.
> 1. Be able to address names using $
> 2. Be able to change individual variables like the NA's to a proper date
> perhaps like
>
> family$Later_Date[[3]] <- toString(format(Sys.time(),"%m/%d/%Y"), width=10)
>
> Thanks, your help would is gratefully accepted,
> Jim
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
More information about the R-help
mailing list