HI this is a fundamental question that I have with R. I know there might be solutions out there to do my task, but I would like to ask why could my function wont work.
嗨这是我与R有关的一个基本问题。我知道可能有解决方案来完成我的任务,但我想问为什么我的功能不会起作用。
I have a data.frame that looks like this
我有一个看起来像这样的data.frame
A B C
1 2 3
2 3 4
and I want to store each of values in A
, B
or C
into individual objects with names A
, B
and C
.
我想将A,B或C中的每个值存储到名称为A,B和C的单个对象中。
So here my function
所以我的功能
splitcolnames<-function(x)
{for (i in colnames(x)){
subset(x, select=c(i))->i}}
}
But it doesn't work. Can someone be kind and point out what did I not get right?
但它不起作用。有人可以善良,并指出我不对的是什么?
2 个解决方案
#1
1
One of the following should do it for you, assuming your data.frame
is named "mydf".
假设您的data.frame名为“mydf”,则下列之一应该为您完成。
lapply(names(mydf), function(x) assign(x, mydf[x], envir = .GlobalEnv))
lapply(names(mydf), function(x) assign(x, mydf[, x], envir = .GlobalEnv))
The first will create single-column data.frame
s, and the second will create vector
s.
第一个将创建单列data.frames,第二个将创建向量。
Example in a clean session:
干净会话中的示例:
> rm(list = ls())
> ls()
character(0)
> mydf <- data.frame(A = c(1, 2), B = c(3, 4))
> mydf
A B
1 1 3
2 2 4
> invisible(lapply(names(mydf), function(x) assign(x, mydf[x], envir = .GlobalEnv)))
> ls()
[1] "A" "B" "mydf"
> A
A
1 1
2 2
> rm(list = ls())
> mydf <- data.frame(A = c(1, 2), B = c(3, 4))
> invisible(lapply(names(mydf), function(x) assign(x, mydf[, x], envir = .GlobalEnv)))
> ls()
[1] "A" "B" "mydf"
> B
[1] 3 4
In the examples above in invisible
to suppress the output.
在上面的例子中,隐形输出无形。
#2
0
while I agree with @RomanLustrik s comment that this is probably not a good idea, the function you are looking for is assign
虽然我同意@RomanLustrik的评论,这可能不是一个好主意,你正在寻找的功能是分配
for (i in colnames(x))
assign(i, subset(x, select=i))
> A
# A
# 1 1
# 2 2
> B
# B
# 1 2
# 2 3
that being said, are you aware of the attach
function?
话虽如此,你知道附加功能吗?
?attach
#1
1
One of the following should do it for you, assuming your data.frame
is named "mydf".
假设您的data.frame名为“mydf”,则下列之一应该为您完成。
lapply(names(mydf), function(x) assign(x, mydf[x], envir = .GlobalEnv))
lapply(names(mydf), function(x) assign(x, mydf[, x], envir = .GlobalEnv))
The first will create single-column data.frame
s, and the second will create vector
s.
第一个将创建单列data.frames,第二个将创建向量。
Example in a clean session:
干净会话中的示例:
> rm(list = ls())
> ls()
character(0)
> mydf <- data.frame(A = c(1, 2), B = c(3, 4))
> mydf
A B
1 1 3
2 2 4
> invisible(lapply(names(mydf), function(x) assign(x, mydf[x], envir = .GlobalEnv)))
> ls()
[1] "A" "B" "mydf"
> A
A
1 1
2 2
> rm(list = ls())
> mydf <- data.frame(A = c(1, 2), B = c(3, 4))
> invisible(lapply(names(mydf), function(x) assign(x, mydf[, x], envir = .GlobalEnv)))
> ls()
[1] "A" "B" "mydf"
> B
[1] 3 4
In the examples above in invisible
to suppress the output.
在上面的例子中,隐形输出无形。
#2
0
while I agree with @RomanLustrik s comment that this is probably not a good idea, the function you are looking for is assign
虽然我同意@RomanLustrik的评论,这可能不是一个好主意,你正在寻找的功能是分配
for (i in colnames(x))
assign(i, subset(x, select=i))
> A
# A
# 1 1
# 2 2
> B
# B
# 1 2
# 2 3
that being said, are you aware of the attach
function?
话虽如此,你知道附加功能吗?
?attach