+ Reply to Thread
Results 1 to 5 of 5

Thread: extract first value from multiple columns

  1. #1
    Points: 471, Level: 9
    Level completed: 43%, Points required for next Level: 29

    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    extract first value from multiple columns




    i am trying to do this in R. it is equivalent to the stata command
    Code: 
    rowfirst
    suppose i have four columns as shown below (T1, T2, T3, T4). I want to create a new column (TX) which contains for each row the first element in columns T1:T4.

    Code: 
    ID T1 T2 T3 T4
    
    A  0  2  4  5
    B  .  .  6  5
    C  .  8  6  1
    D  .  .  .  9
    
    ID TX
    A   0
    B   6
    C   8
    D   9
    thank you everyone!!

  2. #2
    Devorador de queso
    Points: 90,090, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent Poster
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,836
    Thanks
    305
    Thanked 2,604 Times in 2,221 Posts

    Re: extract first value from multiple columns

    Would those dots be stored as NA?
    I don't have emotions and sometimes that makes me very sad.

  3. #3
    Points: 471, Level: 9
    Level completed: 43%, Points required for next Level: 29

    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: extract first value from multiple columns

    Quote Originally Posted by Dason View Post
    Would those dots be stored as NA?
    Yes the dots below are stored as NA. thank you for your reply!

  4. #4
    Points: 2, Level: 1
    Level completed: 3%, Points required for next Level: 48

    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: extract first value from multiple columns

    You can do some thing like this:

    Assuming your data frame is called data

    data$TX=as.vector(data[1,])

    if in case no. of columns is not equal to no. of rows you can use below command it will replace empty values in TX with NA

    iris$v1=as.vector(c(iris[1,],rep(NA,nrow(iris)-ncol(iris))))

  5. #5
    TS Contributor
    Points: 11,424, Level: 70
    Level completed: 44%, Points required for next Level: 226
    rogojel's Avatar
    Location
    I work in Europe, live in Hungary
    Posts
    1,416
    Thanks
    157
    Thanked 325 Times in 305 Posts

    Re: extract first value from multiple columns


    hi,
    you would need to define a function to return the index of the first non NA in a vector, then use apply (or aggregate) on the data feame.

    My shot at the function would be

    NonNA= function(v){
    return (match(0, 1:length(v)*is.na(v))
    }

    to return the first value instead of the index:

    FirstNA= function(v){
    ind= (match(0, 1:length(v)*is.na(v))
    return(v[ind])
    }

    regards

+ Reply to Thread

           




Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats