Passing variable name

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Passing variable name

Seyit Ali KAYIS-2
Dear R users,

 �

I have a data frame as below. In part 1, I have created a table for Gender and Hand, performed Chi-Square test and made graph using ggplot.

 �

I want to replace the original variable names (Gender and Hand) with V1 and V2 and to be able to perform those things again as in #part 2. Is there a way to be able to replace the original names?

 �

Any help is deeply appreciated

 �

Kind Regards

 �

Seyit Ali

 �

#############################################################################

 �

MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",        "M",      "M",              "M",      "M",      "M",      "M",      "F",        "F"),

                   "Hand" = c("R",           "R",        "L",        "L",        "R",        "R",        "L",              "L",        "R",        "R",        "L",        "L"),

                   "Gr" = c(1,      2,           1,           2,           1,           2,           1,           2,              1,           2,           1,           2) )

                     

MyData <- within(MyData, {

  Gender  <- factor(Gender)

  Hand <- factor(Hand)

  Gr   <- factor(Gr)

}

)

 �

str(MyData)

 �

library(ggplot2)

 �

#################     Part 1  #########################################

 �

MyT <- table(MyData$Gender, MyData$Hand)

print(MyT)

 �

MyChi<- chisq.test(MyT)

print(MyChi)

dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))

name2<- c("Gender", "Hand", "Frequency")                                          

names(dMyT) <- name2

 �

ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +

    geom_bar(position="dodge", stat="identity")

 �

#################  Part 2   ################

 �

# I want to be able to pass Gender and Hand as V1 and V2 , respectively to

# table, Chi-Square test and ggplot

 �

V1 <- "Gender"

V2 <- "Hand"

 �

MyT2 <- table(MyData$V1, MyData$V2)

 �

print(MyT)

 �

MyChi<- chisq.test(MyT)

print(MyChi)

dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA = "ifany"))))

name2<- c(V1, V2, "Frequency")                                          

names(dMyT) <- name2

 �

ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +

    geom_bar(position="dodge", stat="identity")

 �

 �

 �

 �

----------------------------------------------------

Dr. Seyit Ali KAYIS

Bolu Abant Izzet Baysal University, Faculty of Medicine

Department of Biostatistics and Medical Informatics

Bolu, Turkey

 

 <mailto:[hidden email]> [hidden email],  <mailto:[hidden email]> [hidden email]

Tel: +90 374 254 30 32 Mobile: +90 535 587 1139

 

Greetings from Bolu, Turkey

------------------------------------------------------

 �


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Passing variable name

Bert Gunter-2
This is a *plain text* list. As you can see from the included text that I
received,  the HTML version that you sent was somewhat mangled by the
server. I do not know whether or not enough got through for you to get a
helpful reply, but if not, re-send *to the list, not me* in *plain text*.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Dec 27, 2020 at 12:25 PM Seyit Ali KAYIS <[hidden email]>
wrote:

> Dear R users,
>
>  �
>
> I have a data frame as below. In part 1, I have created a table for Gender
> and Hand, performed Chi-Square test and made graph using ggplot.
>
>  �
>
> I want to replace the original variable names (Gender and Hand) with V1
> and V2 and to be able to perform those things again as in #part 2. Is there
> a way to be able to replace the original names?
>
>  �
>
> Any help is deeply appreciated
>
>  �
>
> Kind Regards
>
>  �
>
> Seyit Ali
>
>  �
>
>
> #############################################################################
>
>  �
>
> MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",
>   "M",      "M",              "M",      "M",      "M",      "M",      "F",
>       "F"),
>
>                    "Hand" = c("R",           "R",        "L",        "L",
>       "R",        "R",        "L",              "L",        "R",
> "R",        "L",        "L"),
>
>                    "Gr" = c(1,      2,           1,           2,
>  1,           2,           1,           2,              1,           2,
>        1,           2) )
>
>
>
> MyData <- within(MyData, {
>
>   Gender  <- factor(Gender)
>
>   Hand <- factor(Hand)
>
>   Gr   <- factor(Gr)
>
> }
>
> )
>
>  �
>
> str(MyData)
>
>  �
>
> library(ggplot2)
>
>  �
>
> #################     Part 1  #########################################
>
>  �
>
> MyT <- table(MyData$Gender, MyData$Hand)
>
> print(MyT)
>
>  �
>
> MyChi<- chisq.test(MyT)
>
> print(MyChi)
>
> dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand,
> useNA = "ifany"))))
>
> name2<- c("Gender", "Hand", "Frequency")
>
>
> names(dMyT) <- name2
>
>  �
>
> ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +
>
>     geom_bar(position="dodge", stat="identity")
>
>  �
>
> #################  Part 2   ################
>
>  �
>
> # I want to be able to pass Gender and Hand as V1 and V2 , respectively to
>
> # table, Chi-Square test and ggplot
>
>  �
>
> V1 <- "Gender"
>
> V2 <- "Hand"
>
>  �
>
> MyT2 <- table(MyData$V1, MyData$V2)
>
>  �
>
> print(MyT)
>
>  �
>
> MyChi<- chisq.test(MyT)
>
> print(MyChi)
>
> dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA =
> "ifany"))))
>
> name2<- c(V1, V2, "Frequency")
>
> names(dMyT) <- name2
>
>  �
>
> ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +
>
>     geom_bar(position="dodge", stat="identity")
>
>  �
>
>  �
>
>  �
>
>  �
>
> ----------------------------------------------------
>
> Dr. Seyit Ali KAYIS
>
> Bolu Abant Izzet Baysal University, Faculty of Medicine
>
> Department of Biostatistics and Medical Informatics
>
> Bolu, Turkey
>
>
>
>  <mailto:[hidden email]> [hidden email],  <mailto:
> [hidden email]> [hidden email]
>
> Tel: +90 374 254 30 32 Mobile: +90 535 587 1139
>
>
>
> Greetings from Bolu, Turkey
>
> ------------------------------------------------------
>
>  �
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Passing variable name

Jim Lemon-4
In reply to this post by Seyit Ali KAYIS-2
Sorry, Bert, I also forgot to copy to the list.

Hi Seyet Ali,
If you have created the data frame, you probably know that "Gender",
"Hand" and "Gr" are the names of the vectors of values that are joined
in the data frame "MyData". You now wish to change those names. I
don't know why you would change the names to obscure ones like "V1",
but I suggest a look at the "names" function. The answer to your
question should now be clear to you.

Jim

On Mon, Dec 28, 2020 at 8:47 AM Jim Lemon <[hidden email]> wrote:

>
> Hi Seyet Ali,
> If you have created the data frame, you probably know that "Gender",
> "Hand" and "Gr" are the names of the vectors of values that are joined
> in the data frame "MyData". You now wish to change those names. I
> don't know why you would change the names to obscure ones like "V1",
> but I suggest a look at the "names" function. The answer to your
> question should now be clear to you.
>
> Jim
>
> On Mon, Dec 28, 2020 at 7:25 AM Seyit Ali KAYIS
> <[hidden email]> wrote:
> >
> > Dear R users,
> >
> >  �
> >
> > I have a data frame as below. In part 1, I have created a table for Gender and Hand, performed Chi-Square test and made graph using ggplot.
> >
> >  �
> >
> > I want to replace the original variable names (Gender and Hand) with V1 and V2 and to be able to perform those things again as in #part 2. Is there a way to be able to replace the original names?
> >
> >  �
> >
> > Any help is deeply appreciated
> >
> >  �
> >
> > Kind Regards
> >
> >  �
> >
> > Seyit Ali
> >
> >  �
> >
> > #############################################################################
> >
> >  �
> >
> > MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",        "M",      "M",              "M",      "M",      "M",      "M",      "F",        "F"),
> >
> >                    "Hand" = c("R",           "R",        "L",        "L",        "R",        "R",        "L",              "L",        "R",        "R",        "L",        "L"),
> >
> >                    "Gr" = c(1,      2,           1,           2,           1,           2,           1,           2,              1,           2,           1,           2) )
> >
> >
> >
> > MyData <- within(MyData, {
> >
> >   Gender  <- factor(Gender)
> >
> >   Hand <- factor(Hand)
> >
> >   Gr   <- factor(Gr)
> >
> > }
> >
> > )
> >
> >  �
> >
> > str(MyData)
> >
> >  �
> >
> > library(ggplot2)
> >
> >  �
> >
> > #################     Part 1  #########################################
> >
> >  �
> >
> > MyT <- table(MyData$Gender, MyData$Hand)
> >
> > print(MyT)
> >
> >  �
> >
> > MyChi<- chisq.test(MyT)
> >
> > print(MyChi)
> >
> > dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))
> >
> > name2<- c("Gender", "Hand", "Frequency")
> >
> > names(dMyT) <- name2
> >
> >  �
> >
> > ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +
> >
> >     geom_bar(position="dodge", stat="identity")
> >
> >  �
> >
> > #################  Part 2   ################
> >
> >  �
> >
> > # I want to be able to pass Gender and Hand as V1 and V2 , respectively to
> >
> > # table, Chi-Square test and ggplot
> >
> >  �
> >
> > V1 <- "Gender"
> >
> > V2 <- "Hand"
> >
> >  �
> >
> > MyT2 <- table(MyData$V1, MyData$V2)
> >
> >  �
> >
> > print(MyT)
> >
> >  �
> >
> > MyChi<- chisq.test(MyT)
> >
> > print(MyChi)
> >
> > dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA = "ifany"))))
> >
> > name2<- c(V1, V2, "Frequency")
> >
> > names(dMyT) <- name2
> >
> >  �
> >
> > ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +
> >
> >     geom_bar(position="dodge", stat="identity")
> >
> >  �
> >
> >  �
> >
> >  �
> >
> >  �
> >
> > ----------------------------------------------------
> >
> > Dr. Seyit Ali KAYIS
> >
> > Bolu Abant Izzet Baysal University, Faculty of Medicine
> >
> > Department of Biostatistics and Medical Informatics
> >
> > Bolu, Turkey
> >
> >
> >
> >  <mailto:[hidden email]> [hidden email],  <mailto:[hidden email]> [hidden email]
> >
> > Tel: +90 374 254 30 32 Mobile: +90 535 587 1139
> >
> >
> >
> > Greetings from Bolu, Turkey
> >
> > ------------------------------------------------------
> >
> >  �
> >
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Passing variable name

Rui Barradas
Hello,

Or maybe V1 and V2 are variables that hold column names?
If so see thess StackOverflow posts [1], [2]. The trick is to use [[,
not $ to extractthe columns.


V1 <- "Gender"
V2 <- "Hand"

MyT2 <- table(MyData[[V1]], MyData[[V2]])


[1]:
https://stackoverflow.com/questions/18222286/dynamically-select-data-frame-columns-using-and-a-vector-of-column-names

[2]:
https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el


Hope this helps,

Rui Barradas


Às 21:48 de 27/12/20, Jim Lemon escreveu:

> Sorry, Bert, I also forgot to copy to the list.
>
> Hi Seyet Ali,
> If you have created the data frame, you probably know that "Gender",
> "Hand" and "Gr" are the names of the vectors of values that are joined
> in the data frame "MyData". You now wish to change those names. I
> don't know why you would change the names to obscure ones like "V1",
> but I suggest a look at the "names" function. The answer to your
> question should now be clear to you.
>
> Jim
>
> On Mon, Dec 28, 2020 at 8:47 AM Jim Lemon <[hidden email]> wrote:
>>
>> Hi Seyet Ali,
>> If you have created the data frame, you probably know that "Gender",
>> "Hand" and "Gr" are the names of the vectors of values that are joined
>> in the data frame "MyData". You now wish to change those names. I
>> don't know why you would change the names to obscure ones like "V1",
>> but I suggest a look at the "names" function. The answer to your
>> question should now be clear to you.
>>
>> Jim
>>
>> On Mon, Dec 28, 2020 at 7:25 AM Seyit Ali KAYIS
>> <[hidden email]> wrote:
>>>
>>> Dear R users,
>>>
>>>   �
>>>
>>> I have a data frame as below. In part 1, I have created a table for Gender and Hand, performed Chi-Square test and made graph using ggplot.
>>>
>>>   �
>>>
>>> I want to replace the original variable names (Gender and Hand) with V1 and V2 and to be able to perform those things again as in #part 2. Is there a way to be able to replace the original names?
>>>
>>>   �
>>>
>>> Any help is deeply appreciated
>>>
>>>   �
>>>
>>> Kind Regards
>>>
>>>   �
>>>
>>> Seyit Ali
>>>
>>>   �
>>>
>>> #############################################################################
>>>
>>>   �
>>>
>>> MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",        "M",      "M",              "M",      "M",      "M",      "M",      "F",        "F"),
>>>
>>>                     "Hand" = c("R",           "R",        "L",        "L",        "R",        "R",        "L",              "L",        "R",        "R",        "L",        "L"),
>>>
>>>                     "Gr" = c(1,      2,           1,           2,           1,           2,           1,           2,              1,           2,           1,           2) )
>>>
>>>
>>>
>>> MyData <- within(MyData, {
>>>
>>>    Gender  <- factor(Gender)
>>>
>>>    Hand <- factor(Hand)
>>>
>>>    Gr   <- factor(Gr)
>>>
>>> }
>>>
>>> )
>>>
>>>   �
>>>
>>> str(MyData)
>>>
>>>   �
>>>
>>> library(ggplot2)
>>>
>>>   �
>>>
>>> #################     Part 1  #########################################
>>>
>>>   �
>>>
>>> MyT <- table(MyData$Gender, MyData$Hand)
>>>
>>> print(MyT)
>>>
>>>   �
>>>
>>> MyChi<- chisq.test(MyT)
>>>
>>> print(MyChi)
>>>
>>> dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))
>>>
>>> name2<- c("Gender", "Hand", "Frequency")
>>>
>>> names(dMyT) <- name2
>>>
>>>   �
>>>
>>> ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +
>>>
>>>      geom_bar(position="dodge", stat="identity")
>>>
>>>   �
>>>
>>> #################  Part 2   ################
>>>
>>>   �
>>>
>>> # I want to be able to pass Gender and Hand as V1 and V2 , respectively to
>>>
>>> # table, Chi-Square test and ggplot
>>>
>>>   �
>>>
>>> V1 <- "Gender"
>>>
>>> V2 <- "Hand"
>>>
>>>   �
>>>
>>> MyT2 <- table(MyData$V1, MyData$V2)
>>>
>>>   �
>>>
>>> print(MyT)
>>>
>>>   �
>>>
>>> MyChi<- chisq.test(MyT)
>>>
>>> print(MyChi)
>>>
>>> dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA = "ifany"))))
>>>
>>> name2<- c(V1, V2, "Frequency")
>>>
>>> names(dMyT) <- name2
>>>
>>>   �
>>>
>>> ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +
>>>
>>>      geom_bar(position="dodge", stat="identity")
>>>
>>>   �
>>>
>>>   �
>>>
>>>   �
>>>
>>>   �
>>>
>>> ----------------------------------------------------
>>>
>>> Dr. Seyit Ali KAYIS
>>>
>>> Bolu Abant Izzet Baysal University, Faculty of Medicine
>>>
>>> Department of Biostatistics and Medical Informatics
>>>
>>> Bolu, Turkey
>>>
>>>
>>>
>>>   <mailto:[hidden email]> [hidden email],  <mailto:[hidden email]> [hidden email]
>>>
>>> Tel: +90 374 254 30 32 Mobile: +90 535 587 1139
>>>
>>>
>>>
>>> Greetings from Bolu, Turkey
>>>
>>> ------------------------------------------------------
>>>
>>>   �
>>>
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Passing variable name

Seyit Ali KAYIS-2
In reply to this post by Bert Gunter-2
Hi Bert,

 

Thanks a lot for informing me regarding the html format of my email.

 

I also would like to thank to Erdogan CEVHER and Jim LEMON for their kind reply/suggestions. Yes I am aware of names function in R which is not the one I am looking for in here. Let me try to explain in another way.

 

The below part includes data generation, making cross-tab, Chi-Squared test and bar plot through ggplot.

 

#######################################################################

MyData<-data.frame("Gender" = c("F", "F", "F", "F", "M", "M", "M", "M", "M",     "M", "F", "F"),

                   "Hand" = c("R",   "R", "L", "L", "R", "R", "L", "L", "R",     "R", "L", "L"),

                   "Gr" = c(1,  2,   1,   2,   1,   2,   1,   2,   1,   2,     1,   2) )



MyData <- within(MyData, {

  Gender  <- factor(Gender)

  Hand <- factor(Hand)

  Gr   <- factor(Gr)

}

)



str(MyData)



library(ggplot2)

 

#################     Part 1   #########################################



MyT <- table(MyData$Gender, MyData$Hand)

print(MyT)



MyChi<- chisq.test(MyT)

print(MyChi)



dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))



name2<- c("Gender", "Hand", "Frequency")

names(dMyT) <- name2



ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +

        geom_bar(position="dodge", stat="identity")



###############################################



Let’s say I have hundreds of variables (e.g SNP data). By using above codes I can perform what I need. However , I need to copy/paste variable name(s) for making table, Chi-Square test, and ggplot. This increase the chance of incorrectly copying/pasting variable name(s). What I can do is define variable name(s) earlier and pass that names to making table, Chi-Square test, and ggplot part. I believe there is a way to do it. I tried “paste” function (as below), but it did not work either.



Any comment/help is deeply appreciated.



Kind Regards



Seyit Ali



##############################################

V1 <- "Gender"

V2 <- "Hand"



MyT2 <- table(paste('MyData$',V1), paste('MyData$',V2) )

print(MyT)



MyChi<- chisq.test(MyT)

print(MyChi)



dMyT <- data.frame(as.table(as.matrix(table(paste('MyData$',V1), paste('MyData$',V2), useNA = "ifany"))))

name2<- c(V1, V2, "Frequency")

names(dMyT) <- name2



ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +

     geom_bar(position="dodge", stat="identity")



#################################################









From: Bert Gunter [mailto:[hidden email]]
Sent: Monday, 28 December 2020 12:08 AM
To: [hidden email]
Cc: R-help <[hidden email]>
Subject: Re: [R] Passing variable name



This is a *plain text* list. As you can see from the included text that I received,  the HTML version that you sent was somewhat mangled by the server. I do not know whether or not enough got through for you to get a helpful reply, but if not, re-send *to the list, not me* in *plain text*.  




Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )





On Sun, Dec 27, 2020 at 12:25 PM Seyit Ali KAYIS <[hidden email] <mailto:[hidden email]> > wrote:

Dear R users,

 �

I have a data frame as below. In part 1, I have created a table for Gender and Hand, performed Chi-Square test and made graph using ggplot.

 �

I want to replace the original variable names (Gender and Hand) with V1 and V2 and to be able to perform those things again as in #part 2. Is there a way to be able to replace the original names?

 �

Any help is deeply appreciated

 �

Kind Regards

 �

Seyit Ali

 �

#############################################################################

 �

MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",        "M",      "M",              "M",      "M",      "M",      "M",      "F",        "F"),

                   "Hand" = c("R",           "R",        "L",        "L",        "R",        "R",        "L",              "L",        "R",        "R",        "L",        "L"),

                   "Gr" = c(1,      2,           1,           2,           1,           2,           1,           2,              1,           2,           1,           2) )



MyData <- within(MyData, {

  Gender  <- factor(Gender)

  Hand <- factor(Hand)

  Gr   <- factor(Gr)

}

)

 �

str(MyData)

 �

library(ggplot2)

 �

#################     Part 1  #########################################

 �

MyT <- table(MyData$Gender, MyData$Hand)

print(MyT)

 �

MyChi<- chisq.test(MyT)

print(MyChi)

dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))

name2<- c("Gender", "Hand", "Frequency")                                          

names(dMyT) <- name2

 �

ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +

    geom_bar(position="dodge", stat="identity")

 �

#################  Part 2   ################

 �

# I want to be able to pass Gender and Hand as V1 and V2 , respectively to

# table, Chi-Square test and ggplot

 �

V1 <- "Gender"

V2 <- "Hand"

 �

MyT2 <- table(MyData$V1, MyData$V2)

 �

print(MyT)

 �

MyChi<- chisq.test(MyT)

print(MyChi)

dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA = "ifany"))))

name2<- c(V1, V2, "Frequency")                                          

names(dMyT) <- name2

 �

ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +

    geom_bar(position="dodge", stat="identity")

 �

 �

 �

 �

----------------------------------------------------

Dr. Seyit Ali KAYIS

Bolu Abant Izzet Baysal University, Faculty of Medicine

Department of Biostatistics and Medical Informatics

Bolu, Turkey



 <mailto:[hidden email] <mailto:[hidden email]> > [hidden email] <mailto:[hidden email]> ,  <mailto:[hidden email] <mailto:[hidden email]> > [hidden email] <mailto:[hidden email]>

Tel: +90 374 254 30 32 Mobile: +90 535 587 1139



Greetings from Bolu, Turkey

------------------------------------------------------

 �


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] <mailto:[hidden email]>  mailing list -- To UNSUBSCRIBE and more, see
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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Passing variable name

Rui Barradas
Hello,

In my previous post I had meant something like the following.
The two important parts are the use of `[[` in table() and the use of
get() in the ggplot call.


MyData<-data.frame("Gender" = c("F", "F", "F", "F", "M", "M", "M", "M",
"M",     "M", "F", "F"),
                    "Hand" = c("R",   "R", "L", "L", "R", "R", "L", "L",
"R",     "R", "L", "L"),
                    "Gr" = c(1,  2,   1,   2,   1,   2,   1,   2,   1,
2,     1,   2) )

MyData <- within(MyData, {
   Gender  <- factor(Gender)
   Hand <- factor(Hand)
   Gr   <- factor(Gr)
})

V1 <- "Gender"
V2 <- "Hand"

MyT2 <- table(MyData[[V1]], MyData[[V2]])
MyT2

MyChi <- chisq.test(MyT2)
MyChi

name2 <- c(V1, V2, "Frequency")
dMyT <- as.data.frame(MyT2)
names(dMyT) <- name2

library(ggplot2)

ggplot(data = na.omit(dMyT), aes(get(V1), Frequency, fill = get(V2))) +
   geom_bar(position = "dodge", stat = "identity") +
   labs(x = V1, fill = V2)


Is this what you want?

Hope this helps,

Rui Barradas

Às 18:31 de 28/12/20, Seyit Ali KAYIS escreveu:

> Hi Bert,
>
>  
>
> Thanks a lot for informing me regarding the html format of my email.
>
>  
>
> I also would like to thank to Erdogan CEVHER and Jim LEMON for their kind reply/suggestions. Yes I am aware of names function in R which is not the one I am looking for in here. Let me try to explain in another way.
>
>  
>
> The below part includes data generation, making cross-tab, Chi-Squared test and bar plot through ggplot.
>
>  
>
> #######################################################################
>
> MyData<-data.frame("Gender" = c("F", "F", "F", "F", "M", "M", "M", "M", "M",     "M", "F", "F"),
>
>                     "Hand" = c("R",   "R", "L", "L", "R", "R", "L", "L", "R",     "R", "L", "L"),
>
>                     "Gr" = c(1,  2,   1,   2,   1,   2,   1,   2,   1,   2,     1,   2) )
>
>
>
> MyData <- within(MyData, {
>
>    Gender  <- factor(Gender)
>
>    Hand <- factor(Hand)
>
>    Gr   <- factor(Gr)
>
> }
>
> )
>
>
>
> str(MyData)
>
>
>
> library(ggplot2)
>
>    
>
> #################     Part 1   #########################################
>
>
>
> MyT <- table(MyData$Gender, MyData$Hand)
>
> print(MyT)
>
>
>
> MyChi<- chisq.test(MyT)
>
> print(MyChi)
>
>
>
> dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))
>
>
>
> name2<- c("Gender", "Hand", "Frequency")
>
> names(dMyT) <- name2
>
>
>
> ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +
>
>          geom_bar(position="dodge", stat="identity")
>
>
>
> ###############################################
>
>
>
> Let’s say I have hundreds of variables (e.g SNP data). By using above codes I can perform what I need. However , I need to copy/paste variable name(s) for making table, Chi-Square test, and ggplot. This increase the chance of incorrectly copying/pasting variable name(s). What I can do is define variable name(s) earlier and pass that names to making table, Chi-Square test, and ggplot part. I believe there is a way to do it. I tried “paste” function (as below), but it did not work either.
>
>
>
> Any comment/help is deeply appreciated.
>
>
>
> Kind Regards
>
>
>
> Seyit Ali
>
>
>
> ##############################################
>
> V1 <- "Gender"
>
> V2 <- "Hand"
>
>
>
> MyT2 <- table(paste('MyData$',V1), paste('MyData$',V2) )
>
> print(MyT)
>
>
>
> MyChi<- chisq.test(MyT)
>
> print(MyChi)
>
>
>
> dMyT <- data.frame(as.table(as.matrix(table(paste('MyData$',V1), paste('MyData$',V2), useNA = "ifany"))))
>
> name2<- c(V1, V2, "Frequency")
>
> names(dMyT) <- name2
>
>
>
> ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +
>
>       geom_bar(position="dodge", stat="identity")
>
>
>
> #################################################
>
>
>
>
>
>
>
>
>
> From: Bert Gunter [mailto:[hidden email]]
> Sent: Monday, 28 December 2020 12:08 AM
> To: [hidden email]
> Cc: R-help <[hidden email]>
> Subject: Re: [R] Passing variable name
>
>
>
> This is a *plain text* list. As you can see from the included text that I received,  the HTML version that you sent was somewhat mangled by the server. I do not know whether or not enough got through for you to get a helpful reply, but if not, re-send *to the list, not me* in *plain text*.
>
>
>
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
>
>
>
> On Sun, Dec 27, 2020 at 12:25 PM Seyit Ali KAYIS <[hidden email] <mailto:[hidden email]> > wrote:
>
> Dear R users,
>
>   �
>
> I have a data frame as below. In part 1, I have created a table for Gender and Hand, performed Chi-Square test and made graph using ggplot.
>
>   �
>
> I want to replace the original variable names (Gender and Hand) with V1 and V2 and to be able to perform those things again as in #part 2. Is there a way to be able to replace the original names?
>
>   �
>
> Any help is deeply appreciated
>
>   �
>
> Kind Regards
>
>   �
>
> Seyit Ali
>
>   �
>
> #############################################################################
>
>   �
>
> MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",        "M",      "M",              "M",      "M",      "M",      "M",      "F",        "F"),
>
>                     "Hand" = c("R",           "R",        "L",        "L",        "R",        "R",        "L",              "L",        "R",        "R",        "L",        "L"),
>
>                     "Gr" = c(1,      2,           1,           2,           1,           2,           1,           2,              1,           2,           1,           2) )
>
>
>
> MyData <- within(MyData, {
>
>    Gender  <- factor(Gender)
>
>    Hand <- factor(Hand)
>
>    Gr   <- factor(Gr)
>
> }
>
> )
>
>   �
>
> str(MyData)
>
>   �
>
> library(ggplot2)
>
>   �
>
> #################     Part 1  #########################################
>
>   �
>
> MyT <- table(MyData$Gender, MyData$Hand)
>
> print(MyT)
>
>   �
>
> MyChi<- chisq.test(MyT)
>
> print(MyChi)
>
> dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))
>
> name2<- c("Gender", "Hand", "Frequency")
>
> names(dMyT) <- name2
>
>   �
>
> ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +
>
>      geom_bar(position="dodge", stat="identity")
>
>   �
>
> #################  Part 2   ################
>
>   �
>
> # I want to be able to pass Gender and Hand as V1 and V2 , respectively to
>
> # table, Chi-Square test and ggplot
>
>   �
>
> V1 <- "Gender"
>
> V2 <- "Hand"
>
>   �
>
> MyT2 <- table(MyData$V1, MyData$V2)
>
>   �
>
> print(MyT)
>
>   �
>
> MyChi<- chisq.test(MyT)
>
> print(MyChi)
>
> dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA = "ifany"))))
>
> name2<- c(V1, V2, "Frequency")
>
> names(dMyT) <- name2
>
>   �
>
> ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +
>
>      geom_bar(position="dodge", stat="identity")
>
>   �
>
>   �
>
>   �
>
>   �
>
> ----------------------------------------------------
>
> Dr. Seyit Ali KAYIS
>
> Bolu Abant Izzet Baysal University, Faculty of Medicine
>
> Department of Biostatistics and Medical Informatics
>
> Bolu, Turkey
>
>
>
>   <mailto:[hidden email] <mailto:[hidden email]> > [hidden email] <mailto:[hidden email]> ,  <mailto:[hidden email] <mailto:[hidden email]> > [hidden email] <mailto:[hidden email]>
>
> Tel: +90 374 254 30 32 Mobile: +90 535 587 1139
>
>
>
> Greetings from Bolu, Turkey
>
> ------------------------------------------------------
>
>   �
>
>
>          [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] <mailto:[hidden email]>  mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Passing variable name

R help mailing list-2
In reply to this post by Seyit Ali KAYIS-2
There are endless ways to do what you want, Seyit. If you wish to remain in base R, using the names function on the left-hand side changes the names as in:

names(something) <- c("new", "names")

And in general, you may want to learn how to use an alternate set of methods that work well with ggplot in the tidyverse such as select() that lets you choose which columns of a data.frame (or tibble) to keep while optionally renaming them, and the rename() function and the mutate() function and ways to combine them to do lots of work. They also allow you to not use within() the way you are doing.

It is amusing you want names like V1. Indeed some R functions default to such names.

-----Original Message-----
From: R-help <[hidden email]> On Behalf Of Seyit Ali KAYIS
Sent: Monday, December 28, 2020 1:32 PM
To: 'Bert Gunter' <[hidden email]>; 'R-help' <[hidden email]>; [hidden email]; [hidden email]
Subject: Re: [R] Passing variable name

Hi Bert,

 

Thanks a lot for informing me regarding the html format of my email.

 

I also would like to thank to Erdogan CEVHER and Jim LEMON for their kind reply/suggestions. Yes I am aware of names function in R which is not the one I am looking for in here. Let me try to explain in another way.

 

The below part includes data generation, making cross-tab, Chi-Squared test and bar plot through ggplot.

 

#######################################################################

MyData<-data.frame("Gender" = c("F", "F", "F", "F", "M", "M", "M", "M", "M",     "M", "F", "F"),

                   "Hand" = c("R",   "R", "L", "L", "R", "R", "L", "L", "R",     "R", "L", "L"),

                   "Gr" = c(1,  2,   1,   2,   1,   2,   1,   2,   1,   2,     1,   2) )



MyData <- within(MyData, {

  Gender  <- factor(Gender)

  Hand <- factor(Hand)

  Gr   <- factor(Gr)

}

)



str(MyData)



library(ggplot2)

 

#################     Part 1   #########################################



MyT <- table(MyData$Gender, MyData$Hand)

print(MyT)



MyChi<- chisq.test(MyT)

print(MyChi)



dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))



name2<- c("Gender", "Hand", "Frequency")

names(dMyT) <- name2



ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +

        geom_bar(position="dodge", stat="identity")



###############################################



Let’s say I have hundreds of variables (e.g SNP data). By using above codes I can perform what I need. However , I need to copy/paste variable name(s) for making table, Chi-Square test, and ggplot. This increase the chance of incorrectly copying/pasting variable name(s). What I can do is define variable name(s) earlier and pass that names to making table, Chi-Square test, and ggplot part. I believe there is a way to do it. I tried “paste” function (as below), but it did not work either.



Any comment/help is deeply appreciated.



Kind Regards



Seyit Ali



##############################################

V1 <- "Gender"

V2 <- "Hand"



MyT2 <- table(paste('MyData$',V1), paste('MyData$',V2) )

print(MyT)



MyChi<- chisq.test(MyT)

print(MyChi)



dMyT <- data.frame(as.table(as.matrix(table(paste('MyData$',V1), paste('MyData$',V2), useNA = "ifany"))))

name2<- c(V1, V2, "Frequency")

names(dMyT) <- name2



ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +

     geom_bar(position="dodge", stat="identity")



#################################################









From: Bert Gunter [mailto:[hidden email]]
Sent: Monday, 28 December 2020 12:08 AM
To: [hidden email]
Cc: R-help <[hidden email]>
Subject: Re: [R] Passing variable name



This is a *plain text* list. As you can see from the included text that I received,  the HTML version that you sent was somewhat mangled by the server. I do not know whether or not enough got through for you to get a helpful reply, but if not, re-send *to the list, not me* in *plain text*.  




Bert Gunter

"The trouble with having an open mind is that people keep coming along and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )





On Sun, Dec 27, 2020 at 12:25 PM Seyit Ali KAYIS <[hidden email] <mailto:[hidden email]> > wrote:

Dear R users,

 �

I have a data frame as below. In part 1, I have created a table for Gender and Hand, performed Chi-Square test and made graph using ggplot.

 �

I want to replace the original variable names (Gender and Hand) with V1 and V2 and to be able to perform those things again as in #part 2. Is there a way to be able to replace the original names?

 �

Any help is deeply appreciated

 �

Kind Regards

 �

Seyit Ali

 �

#############################################################################

 �

MyData<-data.frame("Gender" = c("F",     "F",        "F",        "F",        "M",      "M",              "M",      "M",      "M",      "M",      "F",        "F"),

                   "Hand" = c("R",           "R",        "L",        "L",        "R",        "R",        "L",              "L",        "R",        "R",        "L",        "L"),

                   "Gr" = c(1,      2,           1,           2,           1,           2,           1,           2,              1,           2,           1,           2) )



MyData <- within(MyData, {

  Gender  <- factor(Gender)

  Hand <- factor(Hand)

  Gr   <- factor(Gr)

}

)

 �

str(MyData)

 �

library(ggplot2)

 �

#################     Part 1  #########################################

 �

MyT <- table(MyData$Gender, MyData$Hand)

print(MyT)

 �

MyChi<- chisq.test(MyT)

print(MyChi)

dMyT <- data.frame(as.table(as.matrix(table(MyData$Gender, MyData$Hand, useNA = "ifany"))))

name2<- c("Gender", "Hand", "Frequency")                                          

names(dMyT) <- name2

 �

ggplot(data = na.omit(dMyT), aes(fill=Hand, y=Frequency, x=Gender)) +

    geom_bar(position="dodge", stat="identity")

 �

#################  Part 2   ################

 �

# I want to be able to pass Gender and Hand as V1 and V2 , respectively to

# table, Chi-Square test and ggplot

 �

V1 <- "Gender"

V2 <- "Hand"

 �

MyT2 <- table(MyData$V1, MyData$V2)

 �

print(MyT)

 �

MyChi<- chisq.test(MyT)

print(MyChi)

dMyT <- data.frame(as.table(as.matrix(table(MyData$V1, MyData$V2, useNA = "ifany"))))

name2<- c(V1, V2, "Frequency")                                          

names(dMyT) <- name2

 �

ggplot(data = na.omit(dMyT), aes(fill=V2, y=Frequency, x=V1)) +

    geom_bar(position="dodge", stat="identity")

 �

 �

 �

 �

----------------------------------------------------

Dr. Seyit Ali KAYIS

Bolu Abant Izzet Baysal University, Faculty of Medicine

Department of Biostatistics and Medical Informatics

Bolu, Turkey



 <mailto:[hidden email] <mailto:[hidden email]> > [hidden email] <mailto:[hidden email]> ,  <mailto:[hidden email] <mailto:[hidden email]> > [hidden email] <mailto:[hidden email]>

Tel: +90 374 254 30 32 Mobile: +90 535 587 1139



Greetings from Bolu, Turkey

------------------------------------------------------

 �


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] <mailto:[hidden email]>  mailing list -- To UNSUBSCRIBE and more, see
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.


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.