pssguy
pssguy

Reputation: 3515

applying functions to pasted strings in R

I have a dataframe,df, with many columns cola,colb etc each consisting of a sequence of integers 0 or 1

df$cola 
[1] 1 0 0 1 0 1 0 0 etc.

I am using the subSeq function in the doBy package to obtain some sequences and want to apply this to all columns

I have tried putting the columns into a vector

cols <- colnames(df) # "cola" "colb" etc.

and have tried without success this approach

subSeq(get(paste0("df$",cols[1]))) # error object 'df$cola' not found

Could not easily find an equivalent on site via search

Upvotes: 0

Views: 90

Answers (1)

sds
sds

Reputation: 60074

I think you are looking for df[[cols[1]]].

Note that df[["foo"]] is the same as df$foo.

Upvotes: 1

Related Questions