Jason Rupert
2009-Mar-23 17:39 UTC
[R] Replacing a few variable values within a DataFrame...
I would like to replace a few varaibles within a data frame.
For example, in the dataframe below (contrived) I would like to replace the
current housesize value only if the Location is HSV. However, I would like to
leave the other values intact.
I tried "ifelse", but I don't really need the else condition.
test_data2_df<-data.frame(Variables=c("SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage"),HouseSize=c(10, 20, 30, 40, 50, 15, 25,
35, 45, 55, 18, 28, 38, 48, 58), Lot=c(1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 11,
21, 31, 41, 51), Location=c("HSV", "ATH", "HSV",
"ATH", "FLO",
"HSV", "ATH",
"HSV", "ATH", "FLO",
"HSV", "ATH",
"HSV", "ATH", "FLO"))
Moreover, I want to preserve the rest of the values within the data. I thought
some combination using the subset would get me there, but I ended up with a long
and ungangly looking code.
Thanks for any help that is offered.
Jorge Ivan Velez
2009-Mar-23 17:51 UTC
[R] Replacing a few variable values within a DataFrame...
Dear Jason, Try this: index<- with(test_data2_df,Location %in% "HSV") test_data2_df$HouseSize<-ifelse(index,1000,test_data2_df$HouseSize) # I used 1000 as reference test_data2_df HTH, Jorge On Mon, Mar 23, 2009 at 1:39 PM, Jason Rupert <jasonkrupert@yahoo.com>wrote:> > I would like to replace a few varaibles within a data frame. > > For example, in the dataframe below (contrived) I would like to replace the > current housesize value only if the Location is HSV. However, I would like > to leave the other values intact. > > > I tried "ifelse", but I don't really need the else condition. > > test_data2_df<-data.frame(Variables=c("SQR Footage","SQR Footage","SQR > Footage","SQR Footage","SQR Footage","SQR Footage","SQR Footage","SQR > Footage","SQR Footage","SQR Footage","SQR Footage","SQR Footage","SQR > Footage","SQR Footage","SQR Footage"),HouseSize=c(10, 20, 30, 40, 50, 15, > 25, 35, 45, 55, 18, 28, 38, 48, 58), Lot=c(1, 2, 3, 4, 5, 10, 20, 30, 40, > 50, 11, 21, 31, 41, 51), Location=c("HSV", "ATH", "HSV", "ATH", "FLO", > "HSV", "ATH", "HSV", "ATH", "FLO", > "HSV", "ATH", "HSV", "ATH", "FLO")) > > Moreover, I want to preserve the rest of the values within the data. I > thought some combination using the subset would get me there, but I ended up > with a long and ungangly looking code. > > Thanks for any help that is offered. > > ______________________________________________ > R-help@r-project.org 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. >[[alternative HTML version deleted]]
baptiste auguie
2009-Mar-23 17:52 UTC
[R] Replacing a few variable values within a DataFrame...
On 23 Mar 2009, at 17:39, Jason Rupert wrote:> > I would like to replace a few varaibles within a data frame. > > For example, in the dataframe below (contrived) I would like to > replace the current housesize value only if the Location is HSV. > However, I would like to leave the other values intact. >How about, test_data2_df[test_data2_df$Location=="HSV", ] # these are the values to change test_data2_df$housesize[test_data2_df$Location=="HSV"] <- 28.3 # or whatever values HTH, baptiste> > I tried "ifelse", but I don't really need the else condition. > > test_data2_df<-data.frame(Variables=c("SQR Footage","SQR > Footage","SQR Footage","SQR Footage","SQR Footage","SQR > Footage","SQR Footage","SQR Footage","SQR Footage","SQR > Footage","SQR Footage","SQR Footage","SQR Footage","SQR > Footage","SQR Footage"),HouseSize=c(10, 20, 30, 40, 50, 15, 25, 35, > 45, 55, 18, 28, 38, 48, 58), Lot=c(1, 2, 3, 4, 5, 10, 20, 30, 40, > 50, 11, 21, 31, 41, 51), Location=c("HSV", "ATH", "HSV", "ATH", "FLO", > "HSV", "ATH", "HSV", "ATH", "FLO", > "HSV", "ATH", "HSV", "ATH", > "FLO")) > > Moreover, I want to preserve the rest of the values within the > data. I thought some combination using the subset would get me > there, but I ended up with a long and ungangly looking code. > > Thanks for any help that is offered. > > ______________________________________________ > R-help at r-project.org 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
Jason Rupert
2009-Mar-23 18:46 UTC
[R] Replacing a few variable values within a DataFrame...
Phil and all,
Thank you for the responses. That method worked great!
However, I did try to replace the a few of the "Variables" using the
same method and received an error...
test_data2_df[test_data2_df$Location == 'HSV','Variables'] =
"YardSize"
Is there something special I need to do in order to replace the string
characters?
Right now it appears "NA" is being used.
Thank you again.
test_data2_df<-data.frame(Variables=c("SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage","SQR Footage","SQR
Footage","SQR Footage"),
HouseSize=c(10, 20, 30, 40, 50, 15, 25, 35, 45, 55, 18, 28, 38, 48, 58),
Lot=c(1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 11, 21, 31, 41, 51),
Location = c("HSV", "ATH", "HSV", "ATH",
"FLO", "HSV", "ATH", "HSV",
"ATH",
"FLO", "HSV", "ATH", "HSV",
"ATH", "FLO"))
--- On Mon, 3/23/09, Phil Spector <spector at stat.berkeley.edu> wrote:
> From: Phil Spector <spector at stat.berkeley.edu>
> Subject: Re: [R] Replacing a few variable values within a DataFrame...
> To: "Jason Rupert" <jasonkrupert at yahoo.com>
> Date: Monday, March 23, 2009, 12:48 PM
> Jason -
> When you say that you want to change some values (if),
> but
> leave others the same (else) I think that the else is very
> important. Here's how to use ifelse to do what you
> want:
>
> test_data2_df$HouseSize = ifelse(test_data2_df$Location
> == 'HSV',newvalue, test_data2_df$HouseSize)
>
> Here's another way to selectively change values in a
> data frame:
>
> test_data2_df[test_data2_df$Location =>
'HSV','HouseSize'] = newvalues
>
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> spector at stat.berkeley.edu
>
>
>
> On Mon, 23 Mar 2009, Jason Rupert wrote:
>
> >
> > I would like to replace a few varaibles within a data
> frame.
> >
> > For example, in the dataframe below (contrived) I
> would like to replace the current housesize value only if
> the Location is HSV. However, I would like to leave the
> other values intact.
> >
> >
> > I tried "ifelse", but I don't really
> need the else condition.
> >
> > test_data2_df<-data.frame(Variables=c("SQR
> Footage","SQR Footage","SQR
> Footage","SQR Footage","SQR
> Footage","SQR Footage","SQR
> Footage","SQR Footage","SQR
> Footage","SQR Footage","SQR
> Footage","SQR Footage","SQR
> Footage","SQR Footage","SQR
> Footage"),HouseSize=c(10, 20, 30, 40, 50, 15, 25, 35,
> 45, 55, 18, 28, 38, 48, 58), Lot=c(1, 2, 3, 4, 5, 10, 20,
> 30, 40, 50, 11, 21, 31, 41, 51), Location=c("HSV",
> "ATH", "HSV", "ATH",
> "FLO",
> > "HSV",
> "ATH", "HSV", "ATH",
> "FLO",
> > "HSV",
> "ATH", "HSV", "ATH",
> "FLO"))
> >
> > Moreover, I want to preserve the rest of the values
> within the data. I thought some combination using the
> subset would get me there, but I ended up with a long and
> ungangly looking code.
> >
> > Thanks for any help that is offered.
> >
> > ______________________________________________
> > R-help at r-project.org 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.
> >