I have a data set in the following format and want to extract data-value for each combination like A_ALL, B_ALL, A_Part, B_part for stastical analysis.
Currently, what I can do is
A_ALL <- data[data$variable=="All" & data$Set=="A",1]
A_Part <- data[data$variable=="Part" & data$Set=="A",1]
I wonder if there is a more efficient way to extract all these data.
Thanks!
Tong Chen
** Input File format **
value variable Set
24.4003 All A
21.2582 All A
1.91043 All A
34.9642 All B
33.794 All B
16.6093 All B
16.6095 All B
24.4003 Part A
21.2582 Part A
34.9642 Part B
33.794 Part B
16.6093 Part B
You can use split
, which will create a list
of the values you're interested in:
with(mydf[mydf$Set == "A", ], split(value, variable))
# $All
# [1] 24.40030 21.25820 1.91043
#
# $Part
# [1] 24.4003 21.2582
Here, instead of splitting on the entire dataset, I'm splitting on the values where Set == "A"
, as you've indicated you need in your current solution.
Alternatively, if you want to split according to all factors of "Set" and "variable" in one go:
with(mydf, split(value, list(Set, variable)))
# $A.All
# [1] 24.40030 21.25820 1.91043
#
# $B.All
# [1] 34.9642 33.7940 16.6093 16.6095
#
# $A.Part
# [1] 24.4003 21.2582
#
# $B.Part
# [1] 34.9642 33.7940 16.6093
I would recommend sticking with a list
, as I've created above. However, if you really want to have a whole bunch of objects in your workspace, you can use list2env
to extract the list items to your environment:
## I currently only have the original data.frame
ls()
# [1] "mydf"
list2env(with(mydf, split(value, list(Set, variable))), envir=.GlobalEnv)
# <environment: R_GlobalEnv>
ls()
# [1] "A.All" "A.Part" "B.All" "B.Part" "mydf"
A.All
# [1] 24.40030 21.25820 1.91043
A.Part
# [1] 24.4003 21.2582
B.All
# [1] 34.9642 33.7940 16.6093 16.6095
B.Part
# [1] 34.9642 33.7940 16.6093
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.