Mathias
Mathias

Reputation: 47

R: avoid repeating $

I'm new here and new to R and I think I have a simple question but don't know how to name it so I can't find any help by searching the web.

I have a data set and want to form a new Data set with several variables from the first one. The working code looks like this:

em.table2 <- data.frame(em.table$item1,em.table$item2,...[here are some more]...,em.table$item22)

In order to keep it more simple, I want to get rid of the "em.table$"-construction in front of every variable... unfortunately i don't know the function to do so... I tried it like this, but it didn't work (and is a pretty embarrasing try i guess):

em.table2 <- data.frame(em.table$(item1,item2,item3,item4))

Anyone here to help? Thanks a lot!

Upvotes: 0

Views: 525

Answers (2)

Felipe Sulser
Felipe Sulser

Reputation: 1195

Instead of the $ operator, try the following:

em.table2 <- em.table[,c("item1","item2","item3","item4")]

Upvotes: 1

Richard Telford
Richard Telford

Reputation: 9923

Try with

em.table2 <- with(em.table, data.frame(item1, item2, item3, item4))

But if you just want to subset the data, there are better solutions.

Upvotes: 0

Related Questions