An embedded and charset-unspecified text was scrubbed... Name: not available Url: https://stat.ethz.ch/pipermail/r-help/attachments/20061227/eb3c5dfb/attachment.pl
how to transform string to variable name in a fuction?
4 messages · jingjiangyan, Wensui Liu, Gabor Grothendieck +1 more
try ?assign
On 12/26/06, jingjiangyan <jingjiangyan at gmail.com> wrote:
there is a data frame, like this:
df
aa bb 1 a 20.27802 2 b 22.10664 3 c 21.33470 4 a 22.32898 5 b 19.73760 6 c 20.38979 .....(suppressed) what I want to do is to copy the data frame's rows into different data frames according to the levels of 'aa' column,
df.a <- df[df[,1]=='a',] ; df.b <- df[df[,1]=='b',] ; .... df.a
aa bb 1 a 20.27802 4 a 22.32898 ... So, when completed, there should be df.a, df.b,df.c, etc. If we could do this by hand, it is pretty fine. But could I write a loop to do this ? when I tried this using a funciton, there is a problem.
for ( i in levels(df[,1])) {
+ name = paste('df',i,sep='')
+ name <- df[df[,1]==i,]
+ }
name
aa bb 3 c 21.33470 6 c 20.38979
ls()
[1] "df" "i" "name"
i
[1] "c"
there is not data frames df.a, df.b,etc.
Could you please give me some suggestion?
I have found that write a function in R for a beginner is difficult. Is there any tutorial on writing the functions in R?
Furthermore, someone also said that loop is not used as frequently as in other script language (e.g. bash, perl). So, If you have any other smart means do this more efficiently, please let me know, I would appreciate your kindness.
[[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
WenSui Liu A lousy statistician who happens to know a little programming (http://spaces.msn.com/statcompute/blog)
In the following the components of ss are the data frames in question: ss <- split(df, df$aa)
On 12/26/06, jingjiangyan <jingjiangyan at gmail.com> wrote:
there is a data frame, like this:
df
aa bb 1 a 20.27802 2 b 22.10664 3 c 21.33470 4 a 22.32898 5 b 19.73760 6 c 20.38979 .....(suppressed) what I want to do is to copy the data frame's rows into different data frames according to the levels of 'aa' column,
df.a <- df[df[,1]=='a',] ; df.b <- df[df[,1]=='b',] ; .... df.a
aa bb 1 a 20.27802 4 a 22.32898 ... So, when completed, there should be df.a, df.b,df.c, etc. If we could do this by hand, it is pretty fine. But could I write a loop to do this ? when I tried this using a funciton, there is a problem.
for ( i in levels(df[,1])) {
+ name = paste('df',i,sep='')
+ name <- df[df[,1]==i,]
+ }
name
aa bb 3 c 21.33470 6 c 20.38979
ls()
[1] "df" "i" "name"
i
[1] "c"
there is not data frames df.a, df.b,etc.
Could you please give me some suggestion?
I have found that write a function in R for a beginner is difficult. Is there any tutorial on writing the functions in R?
Furthermore, someone also said that loop is not used as frequently as in other script language (e.g. bash, perl). So, If you have any other smart means do this more efficiently, please let me know, I would appreciate your kindness.
[[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
I believe the "split" function should work in this case.
From the help file:
split(x, f, drop = FALSE, ...)
split(x, f, drop = FALSE, ...) <- value
unsplit(value, f, drop = FALSE)
Arguments
|x| vector or data frame containing values to be divided into groups.
|f| a ?factor? in the sense that |as.factor <factor.html>(f)| defines
the grouping, or a list of such factors in which case their interaction
is used for the grouping.
|drop| logical indicating if levels that do not occur should be dropped
(if |f| is a |factor| or a list).
|value| a list of vectors or data frames compatible with a splitting of
|x|. Recycling applies if the lengths do not match.
|...| further potential arguments passed to methods.
Abhijit Dasgupta, Ph.D.
Assistant Professor | Division of Biostatistics
Department of Pharmacology and Experimental Therapeutics | Thomas
Jefferson University
1015 Chestnut St | Suite M100 | Philadelphia, PA 19107
Ph: (215) 503-9201 | Fax: (215) 503-3804
jingjiangyan wrote:
there is a data frame, like this:
df
aa bb 1 a 20.27802 2 b 22.10664 3 c 21.33470 4 a 22.32898 5 b 19.73760 6 c 20.38979 .....(suppressed) what I want to do is to copy the data frame's rows into different data frames according to the levels of 'aa' column,
df.a <- df[df[,1]=='a',] ; df.b <- df[df[,1]=='b',] ; ....
df.a
aa bb 1 a 20.27802 4 a 22.32898 ... So, when completed, there should be df.a, df.b,df.c, etc. If we could do this by hand, it is pretty fine. But could I write a loop to do this ? when I tried this using a funciton, there is a problem.
for ( i in levels(df[,1])) {
+ name = paste('df',i,sep='')
+ name <- df[df[,1]==i,]
+ }
name
aa bb 3 c 21.33470 6 c 20.38979
ls()
[1] "df" "i" "name"
i
[1] "c" there is not data frames df.a, df.b,etc. Could you please give me some suggestion? I have found that write a function in R for a beginner is difficult. Is there any tutorial on writing the functions in R? Furthermore, someone also said that loop is not used as frequently as in other script language (e.g. bash, perl). So, If you have any other smart means do this more efficiently, please let me know, I would appreciate your kindness. [[alternative HTML version deleted]]
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.