开发者

R: How can I use apply on rows of a data.frame and get out $column_name?

开发者 https://www.devze.com 2022-12-24 20:16 出处:网络
I\'m trying to access $a using the following example: df<-data.frame(a=c(\"x\",\"x\",\"y\",\"y\"),b=c(1,2,3,4))

I'm trying to access $a using the following example:

df<-data.frame(a=c("x","x","y","y"),b=c(1,2,3,4))

> df
  a b
1 x 1
2 x 2
3 y 3
4 y 4

test_fun <- function (data.frame_in) {
    print (data.frame_in[1])
    }

I can now access $a if I use an index for the first column:

apply(df, 1, test_fun)

  a 
"x" 
  a 
"x" 
  a 
"y" 
  a 
"y" 
[1] "x" "x" "y" "y"

But I cannot access column $a with the $ notation: error: "$ operator is invalid for atomic vectors"

test_fun_2 <- function (data.frame_in) {
    print (data.frame_in$a)
    }

>apply(df, 1, test_fun_2)
Error in开发者_Go百科 data.frame_in$a : $ operator is invalid for atomic vectors

Is this not possible?


You could use adply from the plyr package instead:

library(plyr)
adply(df, 1, function (data.frame_in) print(data.frame_in$a))


because data.frame.in is not a data.frame:

apply(df, 1, function(v){print(class(v))})

but you can access the named elements with:

test_fun_2 <- function (data.frame_in) {
+     print (data.frame_in['a'])}


Because apply changes the data type in your function:

> apply(df, 1, class)
[1] "character" "character" "character" "character"

> apply(df, 1, colnames)
NULL

Since there are no column names, you can't reference the values with the $ operator.

From the apply documentation:

If X is not an array but has a dimension attribute, apply attempts to coerce it to an array via as.matrix if it is two-dimensional (e.g., data frames) or via as.array.

0

精彩评论

暂无评论...
验证码 换一张
取 消