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 6968431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:27:03+00:00 2026-05-27T16:27:03+00:00

> X1 <- data.frame(A=c(a1,a2,a3,a4)) > X1 A 1 a1 2 a2 3 a3 4

  • 0
> X1 <- data.frame(A=c("a1","a2","a3","a4"))
> X1
   A
1 a1
2 a2
3 a3
4 a4

> y <- data.frame(A=c("b1", " ", " ", "b2"," ", "b3"), B=c("c1","c2","c3","c4","X1","c5"))
> y
   A  B
1 b1 c1
2    c2
3    c3
4 b2 c4
5    X1
6 b3 c5

I want to do three things in table y.
(1) replace X1 in column B of the table y with the entries (i.e. a1…a4) in the Table X1.
(2) fill all empty spaces in column A of table y.
(3) add new column C in table y and fill all with the number “1”

The new table y is expected to be as follows:

   A  B C
1 b1 c1 1
2 b1 c2 1
3 b1 c3 1
4 b2 c4 1
5 b2 a1 1
6 b2 a2 1
7 b2 a3 1
8 b2 a4 1
9 b3 c5 1

Could you mind to suggest effective way of doing this, I have a large number of X1 table, not just one. I have to match the names of these tables with column B of table y. I could do it one-by-one, but I am confident that there are smarter way of doing this. Pls kindly help.

  • 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-05-27T16:27:04+00:00Added an answer on May 27, 2026 at 4:27 pm

    To fill out the A column:

    y$A<-y$A[y$A!=" "][cumsum(y$A!=" ")]
    

    To add the C column (come on, you could’ve found this yourself!):

    y$C=1 #Duh
    

    To expand the B column:
    If the order is not important:

    x1part<-y[rep(which(y$b=="X1"), each=nrow(X1)),]
    x1part$B<-X1$A
    cbind(y[y$b!="X1",], x1part)
    

    If the order is important:

    newy<-y[rep(seq(nrow(y)), ifelse(y$b=="X1", nrow(X1), 1)),]
    newy$B[newy$B=="X1"]<-X1$A
    

    Note that this is untested, but the ideas are there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a data.table and I want to select all the rows
I want to filter rows from a data.frame based on a logical condition. Let's
I want to convert variables into factors using apply() : a <- data.frame(x1 =
I have a data frame with two columns. First column contains categories such as
I have a data frame where one particular column has a set of specific
I have three equal-sized data frames that I want to merge in a particular
I have text data (in R) and want to replace some characters with other
My dataset looks like this: `DSET<-data.frame(cbind(c(rep(V1,3),rep(V2,3),V3), c(rep(c(X1,X2,X3),2),X1), c(rep(1,7))))` `names(DSET)<-c(A,B,C)` `DSET[,3]<-c(1,-2,1,3,-1,2,-3)` With three grouping variables
I have a data.frame with 2 columns: Node A, Node B. Each entry in
I've got a data frame with 2 character columns. I'd like to find the

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.