printing a dataframe and getting the result I want!

Here is the input:
name<-c("Joe", "John", "Nancy")
sex <- c("M", "M", "F")
age <- c(27, 26, 26)
df <- data.frame(name, sex, age)
result <-data.frame(name$age, name$sex)
print(result)

comes back as:
Error in name$age : $ operator is invalid for atomic vectors

but I thought I was creating a dataframe!
Also it automatically prints out the result of the last equation which is kind of strange...

ok. but 'name' contains c("Joe", "John", "Nancy") yet you wrote code to take 'age' from name ... name$age ?
try

result <-data.frame(df$age, df$sex)

or given that they aren't only in df but also in the global environment (i.e. based on how you went about making df)

result <- data.frame(age,sex)

Hello! I figured out to take out the $ and it worked fine. But I was unaware that I was in a global environement. How can I differentiate?
By the way, thanks for your time. It wouldn't be possible to get thru this muck without some direction.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.