John Kane
2007-Oct-02 18:54 UTC
[R] Strange names when creating a data.frame: Difference between <- and =
This is just a curiosity question. Why do the two different syntaxes for df1 and df2 give such different results in the names(dfx)? Thanks df1 <- data.frame(nas = c("A", "B" , "B" ,"C" ,"B", "A", "D"),nums = c(3, 2, 1, 1, 2, 3, 7)) df2 <- data.frame(nas <- c("A", "B" , "B" ,"C" ,"B", "A", "D"),nums <- c(3, 2, 1, 1, 2, 3, 7)) df1; df2>df1nas nums 1 A 3 2 B 2 . .> df2nas....c..A....B....B....C....B....A....D.. nums....c.3..2..1..1..2..3..7. 1 A 3 2 B 3 . .
jim holtman
2007-Oct-02 19:24 UTC
[R] Strange names when creating a data.frame: Difference between <- and =
your two dataframe defintions are different. In the case of df1, you have 'names' the elements, and in the case of df2,you have done an assignment to 'nam' and 'num' (look in the workspace and you will see the object. Since you haven't names the elements in df2, is tries to constuct a name from the expressions that is given (e.g., nums = c(3, 2, 1, 1, 2, 3, 7)), so you get. This is what happens:> make.names("nums <- c(3, 2, 1, 1, 2, 3, 7)")[1] "nums.....c.3.....2.....1....1....2.....3..7." It tries to make a name from the expression. On 10/2/07, John Kane <jrkrideau at yahoo.ca> wrote:> This is just a curiosity question. Why do the two > different syntaxes for df1 and df2 give such different > results in the names(dfx)? > > Thanks > > > df1 <- data.frame(nas = c("A", "B" , "B" ,"C" ,"B", > "A", "D"),nums = c(3, 2, 1, 1, 2, 3, 7)) > > df2 <- data.frame(nas <- c("A", "B" , "B" ,"C" ,"B", > "A", "D"),nums <- c(3, 2, 1, 1, 2, 3, > 7)) > > df1; df2 > > >df1 > nas nums > 1 A 3 > 2 B 2 > . . > > > df2 > > nas....c..A....B....B....C....B....A....D.. > nums....c.3..2..1..1..2..3..7. > 1 A > 3 > 2 B > > 3 > . . > > ______________________________________________ > 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 Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Vladimir Eremeev
2007-Oct-02 19:29 UTC
[R] Strange names when creating a data.frame: Difference between <- and =
John Kane-2 wrote:> > This is just a curiosity question. Why do the two > different syntaxes for df1 and df2 give such different > results in the names(dfx)? > > Thanks > > > df1 <- data.frame(nas = c("A", "B" , "B" ,"C" ,"B", > "A", "D"),nums = c(3, 2, 1, 1, 2, 3, 7)) > > df2 <- data.frame(nas <- c("A", "B" , "B" ,"C" ,"B", > "A", "D"),nums <- c(3, 2, 1, 1, 2, 3, > 7)) > > df1; df2 > >>df1 > nas nums > 1 A 3 > 2 B 2 > . . > >> df2 > > nas....c..A....B....B....C....B....A....D.. > nums....c.3..2..1..1..2..3..7. > 1 A > 3 > 2 B > > 3 > . . >You specify argument names in the first function call in the form agrument.name=value Argument names become column names in the data frame. "<-" is the assignment operator, it returns the assigned value, it is not the specification of a function argument. Second function call does not contain argument names, so, they are constructed from expressions. You should have the variables nas and nums in your workspace now. ls() should show them. If you type data.frame (without parentheses, "(" and ")") on the R command prompt it will print the body of this function. You can study it and discover how it works. -- View this message in context: http://www.nabble.com/Strange-names-when-creating-a-data.frame%3A-Difference-between-%3C--and-%3D-tf4557141.html#a13006205 Sent from the R help mailing list archive at Nabble.com.
Prof Brian Ripley
2007-Oct-02 19:31 UTC
[R] Strange names when creating a data.frame: Difference between <- and =
On Tue, 2 Oct 2007, John Kane wrote:> This is just a curiosity question. Why do the two > different syntaxes for df1 and df2 give such different > results in the names(dfx)?Because only in the second case did you specify the names in the call. When you do nas <- c("A" ...) the argument has no name but you create an object called 'nas' in the calling environment (the workspace, it looks like). Perhaps you are confused by thinking '=' is an assignment operator in R, but it is so only when it is not used in any other sense.> > Thanks > > > df1 <- data.frame(nas = c("A", "B" , "B" ,"C" ,"B", > "A", "D"),nums = c(3, 2, 1, 1, 2, 3, 7)) > > df2 <- data.frame(nas <- c("A", "B" , "B" ,"C" ,"B", > "A", "D"),nums <- c(3, 2, 1, 1, 2, 3, > 7)) > > df1; df2 > >> df1 > nas nums > 1 A 3 > 2 B 2 > . . > >> df2 > > nas....c..A....B....B....C....B....A....D.. > nums....c.3..2..1..1..2..3..7. > 1 A > 3 > 2 B > > 3 > . . > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Possibly Parallel Threads
- Subassignments involving NAs in data frames
- difference of two data frames
- Best way/practice to create a new data frame from two given ones with last column computed from the two data frames?
- merging or joining 2 dataframes: merge, rbind.fill, etc.?
- as.data.frame.matrix() returns an invalid object