Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:16:06+00:00 2026-06-13T01:16:06+00:00

I have a simple data frame column transformation which can be done using an

  • 0

I have a simple data frame column transformation which can be done using an if/else loop, but I was wondering if there was a better way to do this.

The initial data frame is,

 df <-data.frame(cbind(x=rep(10:15,3), y=0:8))
 df
    x y
1  10 0
2  11 1
3  12 2
4  13 3
5  14 4
6  15 5
7  10 6
8  11 7
9  12 8
10 13 0
11 14 1
12 15 2
13 10 3
14 11 4
15 12 5
16 13 6
17 14 7
18 15 8

what I need to do is replace the values in column ‘y’ such that

'0' gets replaced with '2',
'1' gets replaced with '2.2',
'2' gets replaced with '2.4',
...
...
'6' gets replaced with '3.2'
'7' gets replaced with '3.3'
'8' gets replaced with '10'

so that I end up with something like,

> df
    x    y
1  10  2.0
2  11  2.2
3  12  2.4
4  13  2.6
5  14  2.8
6  15  3.0
7  10  3.2
8  11  3.3
9  12 10.0
10 13  2.0
11 14  2.2
12 15  2.4
13 10  2.6
14 11  2.8
15 12  3.0
16 13  3.2
17 14  3.3
18 15 10.0

I have searched and found several proposals but couldnt get them to work. One of the attempts was something like,

> levels(factor(df$y)) <- c(2,2.2,2.4,2.6,2.8,3,3.2,3.3,10)

Error in levels(factor(df$y)) <- c(2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.3,  : 
  could not find function "factor<-"

But I get the error message shown above.

Can anyone help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:16:07+00:00Added an answer on June 13, 2026 at 1:16 am

    Use the fact that y+1 is an index for the replacement

    something like

    replacement <- c(2,2.2,2.4,2.6,2.8,3,3.2,3.3,10)
    df <- within(df, z <- replacement[y+1])
    

    Or, using data.table for syntatic sugar and memory efficiency

    library(data.table)
    DT <- as.data.table(df)
    
    DT[, z := replacement[y+1]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping there's a simple answer here but I can't find it anywhere. I have
I have a very simple problem. I have a data frame with multiple rows
I have a simple data model of two tables, email and recipients, email can
I have simple SSIS package which reads data from flat file and insert into
I have a simple jasper report which shows some data from database. The jasper
i am trying to run a simple multiplication of a data.frame column with a
I have a data.frame in R which has been constructed off the Example 1-3
I have a data frame the first columns of which are a sample ID
Suppose I have a data.frame with N rows. The id column has 10 unique
I have a simple dataframe with two columns: df <- data.frame(x = c(1,1,2,2,3), y

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.