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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:37:53+00:00 2026-06-16T17:37:53+00:00

I have a datafile: https://dl.dropbox.com/u/22681355/example.csv Read file: example<-read.csv(example.csv) example<-example[,-1] example[,1] contains a list of

  • 0

I have a datafile:

https://dl.dropbox.com/u/22681355/example.csv

Read file:

example<-read.csv("example.csv")
example<-example[,-1]

example[,1] contains a list of numbers increasing in numerical order.
example[,2] contains another set of numbers

First I would like to identify the numbers in example[,2] that are no listed in example[,1]

diff<-setdiff(example[,2],example[,1])

Now that I know these values I would like to insert them into example[,1] leaving existing values in example[,1] and example[,2] intact.

A short example would be:

Example[,1]   Example[,2]
1             1000
1             50
1             3
1             90
1             25
3             4
5             2
5             7
etc           etc

After I run setdiff() I get the numbers not in the first column but in the second.

Now I would like to place them in example[,1] to produce the following output:

Example[,1]   Example[,2]
1             1000
1             50
1             3
1             90
1             25
2             NA
3             4
4             NA
5             2
5             7
etc           etc

So basically placing them in numerical order but leaving everything else intact.

Part 1 excellently solved by Joris Meys!

I have two further questions:

/////////////////////////////////////////////
////////////////////////////////////////////

1:

Can the same be done if there is an additional third column but I don’t want to do anything with it?

e.g.:

ORIGINAl

 Example[,1]   Example[,2] Example[,3]
 1             1000        37
 1             50          18
 1             3           54
 1             90          72
 1             25          23
 3             4           15
 5             2           20
 5             7           9
 etc           etc

Desired OUTPUT:

Example[,1]   Example[,2]  Example[,3]
1             1000         37
1             50           18
1             3            54
1             90           72
1             25           23
2             NA           NA
3             4            15
4             NA           NA
5             2            20
5             7            19
etc           etc

2:

Instead of adding NA in example[,2] to cases where example[,1] doesnt have the value from example[,2] for example example[,1] doesn’t have number ’30’ then I would like to search for whether example[,2] has number’30’and see what value example[,1] has in that row then add it to example[,2] instead of the NA’s.

for example:

Example[,1]   Example[,2]  Example[,3]
1             1000         37
1             50           18
1             3            54
1             90           72
1             25           23
2             NA           NA
3             4            15
4             NA           NA
5             2            20
5             7            19
etc           etc

Instead of NA’s have:

Example[,1]   Example[,2]  Example[,3]
1             1000         37
1             50           18
1             3            54
1             90           72
1             25           23
2            5            20
3             4            15
4            3           15
5             2            20
5             7            19
etc           etc
  • 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-16T17:37:54+00:00Added an answer on June 16, 2026 at 5:37 pm

    The following approch also works if your matrix has more than two columns. It’s an extension of Joris Meys’ solution.

    Example <- matrix(c(1,1,1,1,1,3,5,5,
                        1000,50,3,90,25,4,2,7,37,18,54,72,23,15,20,9),ncol=3)
    
    
    diffs <- setdiff(Example[,2], Example[,1])
    new_mat <- rbind(Example,
                     matrix(c(diffs,
                              rep(NA, length(diffs) * (ncol(Example) - 1))), 
                            ncol = ncol(Example)))
    solution <- new_mat[order(new_mat[,1]),]
    

    The result:

          [,1] [,2] [,3]
     [1,]    1 1000   37
     [2,]    1   50   18
     [3,]    1    3   54
     [4,]    1   90   72
     [5,]    1   25   23
     [6,]    2   NA   NA
     [7,]    3    4   15
     [8,]    4   NA   NA
     [9,]    5    2   20
    [10,]    5    7    9
    [11,]    7   NA   NA
    [12,]   25   NA   NA
    [13,]   50   NA   NA
    [14,]   90   NA   NA
    [15,] 1000   NA   NA
    

    Once you have created this matrix, it’s easy to generate a new one without NAs:

    solution2 <- solution
    solution2[is.na(solution2)] <- Example[match(sort(diffs), Example[,2]), -2]
    

    The result:

          [,1] [,2] [,3]
     [1,]    1 1000   37
     [2,]    1   50   18
     [3,]    1    3   54
     [4,]    1   90   72
     [5,]    1   25   23
     [6,]    2    5   20
     [7,]    3    4   15
     [8,]    4    3   15
     [9,]    5    2   20
    [10,]    5    7    9
    [11,]    7    5    9
    [12,]   25    1   23
    [13,]   50    1   18
    [14,]   90    1   72
    [15,] 1000    1   37
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data file, which I need to read. I know to read
I have a data file that looks like the following example. I've added '%'
I have a text data file that I likely will read with readLines .
Have file in @attachment . From debug: datafile=>#<ActionDispatch::Http::UploadedFile:0x3eee9c0 @original_filename=filename.jpg, @content_type=image/jpeg, @headers=Content-Disposition: form-data; name=\datafile\; filename=\filename.jpg\\r\nContent-Type:
I have a csv file into which has crept some ^M dos line ends,
I am using the SWFUpload from http://www.anedix.com/data/file/news/realname/swfupload-php-example-v1_4.zip and want to integrate this into my
I have a data file that looks like this: xyz123 2.000 -0.3974 0.0 hij123
I have a data file with the # sign as delimiter, that I would
I have a data file which is comprised of rows of data, newline separated.
I have a data file, a signature file (created by the GnuPG) and a

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.