2

Can someone explain why in the below example the column name for why appears to remain x even though it is clearly named why and can be called as such but not for the column zz?

df<-data.frame(x=1:5,y=1:5)
df$z<-"a"
df$zz<-df$x*df$y
df$why<-df[1]*df[2]

df

df["why"]

2 Answers 2

6

Because you're actually storing a dataframe into why - not a vector.

> str(df)
'data.frame':   5 obs. of  4 variables:
 $ x  : int  1 2 3 4 5
 $ y  : int  1 2 3 4 5
 $ z  : chr  "a" "a" "a" "a" ...
 $ why:'data.frame':    5 obs. of  1 variable:
  ..$ x: int  1 4 9 16 25
> str(df[1]*df[2])
'data.frame':   5 obs. of  1 variable:
 $ x: int  1 4 9 16 25
> str(df[,1] * df[,2])
 int [1:5] 1 4 9 16 25  
> df$why2 <- df[,1]*df[,2]
> df
  x y z  x why2
1 1 1 a  1    1
2 2 2 a  4    4
3 3 3 a  9    9
4 4 4 a 16   16
5 5 5 a 25   25

df[1] returns the first element of df as a sublist. A dataframe is a special type of list which is why you can use this type of indexing to grab columns. However only using the single bracket tells it to return a sublist containing the element of interest (instead of just the element of interest).

Sign up to request clarification or add additional context in comments.

1 Comment

i won't forget my [,] anymore
1

May be your problem is indexing, try this:

df$zz<-df$x*df$y       # this should replace df$zz<-x*y
df$why<-df[,1]*df[,2]  # this repaces df$why<-df[1]*df[2]

df
  x y z zz why
1 1 1 a  1   1
2 2 2 a  4   4
3 3 3 a  9   9
4 4 4 a 16  16
5 5 5 a 25  25

df["why"]
  why
1   1
2   4
3   9
4  16
5  25

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.