Quantcast

relating data in two data frames

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

relating data in two data frames

Sapana Lohani
Hi,


My data.frame "A" has FID like this

FID

a

a
b
b
b
c
c
d
d
d
d

Now my second data.frame "B" has age value for a, b, c, d like

FID Age

a      5

b      7
c      9
d      3

How can search for the Age column in "B" and replace the values in "A" so that my new "A" looks like this

FID Age

a      5
a      5
b      7
b      7
b      7
c      9
c      9
d      3
d      3
d      3
d      3

Thanks you

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: relating data in two data frames

David Winsemius

On Aug 19, 2012, at 5:56 PM, Sapana Lohani wrote:

> Hi,
>
>
> My data.frame "A" has FID like this
>
> FID
>
> a
>
> a
> b
> b
> b
> c
> c
> d
> d
> d
> d
>
> Now my second data.frame "B" has age value for a, b, c, d like
>
> FID Age
>
> a      5
>
> b      7
> c      9
> d      3
>
> How can search for the Age column in "B" and replace the values in  
> "A" so that my new "A" looks like this

? merge

Perhaps

merge(A, B)
# although the ambiguities inherent in posting only screen output  
instead of the greatly-to-be-preferred output of dput() makes this a  
guess.

>
> FID Age
>
> a      5
> a      5
> b      7
> b      7
> b      7
> c      9
> c      9
> d      3
> d      3
> d      3
> d      3
>
> Thanks you
>
> [[alternative HTML version deleted]]

And posting html is a deprecated format for rhelp.
>
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

(Console output does not qualify as reproducible.)


--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: relating data in two data frames

arun kirshna
In reply to this post by Sapana Lohani
Hi,
Try this:
A<-data.frame(FID=c("a","a","b","b","c","c","d","d","d","d"))
B<-read.table(text="
FID Age
a      5
b      7
c      9
d      3
",sep="",header=TRUE)
library(plyr)
join(A,B,type="inner")
Joining by: FID
   FID Age
1    a   5
2    a   5
3    b   7
4    b   7
5    c   9
6    c   9
7    d   3
8    d   3
9    d   3
10   d   3
A.K.



----- Original Message -----
From: Sapana Lohani <[hidden email]>
To: "[hidden email]" <[hidden email]>
Cc:
Sent: Sunday, August 19, 2012 8:56 PM
Subject: [R] relating data in two data frames

Hi,


My data.frame "A" has FID like this

FID

a

a
b
b
b
c
c
d
d
d
d

Now my second data.frame "B" has age value for a, b, c, d like

FID Age

a      5

b      7
c      9
d      3

How can search for the Age column in "B" and replace the values in "A" so that my new "A" looks like this

FID Age

a      5
a      5
b      7
b      7
b      7
c      9
c      9
d      3
d      3
d      3
d      3

Thanks you

    [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

irregular splits in dataframe

Sapana Lohani
In reply to this post by Sapana Lohani
Hello everyone,


My dataframe (Soil Type) looks something like this

Granitic Hills 16-20 PZ
Loamy Upland 16-20 PZ
Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
Loamy Upland 16-20 PZ

I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ??

Thank you
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: irregular splits in dataframe

jholtman
try this:


> x <- scan(text = "Granitic Hills 16-20 PZ
+ Loamy Upland 16-20 PZ
+ Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
+ Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
+ Loamy Upland 16-20 PZ", what = '', sep = '\n')
Read 5 items
> x
[1] "Granitic Hills 16-20 PZ"
[2] "Loamy Upland 16-20 PZ"
[3] "Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ"
[4] "Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ"
[5] "Loamy Upland 16-20 PZ"
> y <- strsplit(x, '/')
> # now put NA is there is only one entry
> y <- lapply(y, function(.line){
+     if (length(.line) == 1) .line <- c(.line, NA)  # add NA
+     .line
+ })
> do.call(rbind, y)
     [,1]                          [,2]
[1,] "Granitic Hills 16-20 PZ"     NA
[2,] "Loamy Upland 16-20 PZ"       NA
[3,] "Sandy Loam Upland 12-16 PZ " " Sandy Loam, Deep 12-16 PZ"
[4,] "Loamy Upland 12-16 PZ "      " Sandy Loam Upland 12-16 PZ"
[5,] "Loamy Upland 16-20 PZ"       NA
>


On Tue, Aug 21, 2012 at 1:54 AM, Sapana Lohani <[hidden email]> wrote:

> Hello everyone,
>
>
> My dataframe (Soil Type) looks something like this
>
> Granitic Hills 16-20 PZ
> Loamy Upland 16-20 PZ
> Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
> Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
> Loamy Upland 16-20 PZ
>
> I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ??
>
> Thank you
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: irregular splits in dataframe

Noia Raindrops
In reply to this post by Sapana Lohani
How about this?

read.table(text = "Granitic Hills 16-20 PZ
Loamy Upland 16-20 PZ
Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
Loamy Upland 16-20 PZ", header = FALSE, sep = "/", fill = TRUE, na.strings = "", strip.white = TRUE)
##                           V1                         V2
## 1    Granitic Hills 16-20 PZ                       <NA>
## 2      Loamy Upland 16-20 PZ                       <NA>
## 3 Sandy Loam Upland 12-16 PZ  Sandy Loam, Deep 12-16 PZ
## 4      Loamy Upland 12-16 PZ Sandy Loam Upland 12-16 PZ
## 5      Loamy Upland 16-20 PZ                       <NA>

--
Noia Raindrops
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: irregular splits in dataframe

arun kirshna
In reply to this post by Sapana Lohani
Hi,
Try this:
dat1 <- readLines(textConnection("Granitic Hills 16-20 PZ
 Loamy Upland 16-20 PZ
 Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
 Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
 Loamy Upland 16-20 PZ")) 

dat3<-strsplit(dat1,"/")
 data.frame(col1=do.call(rbind,lapply(dat3,`[`,1)),col2=do.call(rbind,lapply(dat3,`[`,2)))
#                          col1                        col2
#1      Granitic Hills 16-20 PZ                        <NA>
#2        Loamy Upland 16-20 PZ                        <NA>
#3  Sandy Loam Upland 12-16 PZ    Sandy Loam, Deep 12-16 PZ
#4       Loamy Upland 12-16 PZ   Sandy Loam Upland 12-16 PZ
#5        Loamy Upland 16-20 PZ                        <NA> 

A.K.

----- Original Message -----
From: Sapana Lohani <[hidden email]>
To: "[hidden email]" <[hidden email]>
Cc:
Sent: Tuesday, August 21, 2012 1:54 AM
Subject: [R] irregular splits in dataframe

Hello everyone,


My dataframe (Soil Type) looks something like this

Granitic Hills 16-20 PZ
Loamy Upland 16-20 PZ
Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
Loamy Upland 16-20 PZ

I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ??

Thank you
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: irregular splits in dataframe

arun kirshna
In reply to this post by Sapana Lohani
Hi,


Slight modification to my earlier code to make it more simple.
dat1 <- readLines(textConnection("Granitic Hills 16-20 PZ
 Loamy Upland 16-20 PZ
 Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
 Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
 Loamy Upland 16-20 PZ"))

dat3<-strsplit(dat1,"/")

data.frame(do.call(rbind,lapply(dat3,`[`,c(1,2))))
#                            X1                          X2
#1      Granitic Hills 16-20 PZ                        <NA>
#2        Loamy Upland 16-20 PZ                        <NA>
#3  Sandy Loam Upland 12-16 PZ    Sandy Loam, Deep 12-16 PZ
#4       Loamy Upland 12-16 PZ   Sandy Loam Upland 12-16 PZ
#5        Loamy Upland 16-20 PZ                        <NA>
A.K.

----- Original Message -----
From: Sapana Lohani <[hidden email]>
To: "[hidden email]" <[hidden email]>
Cc:
Sent: Tuesday, August 21, 2012 1:54 AM
Subject: [R] irregular splits in dataframe

Hello everyone,


My dataframe (Soil Type) looks something like this

Granitic Hills 16-20 PZ
Loamy Upland 16-20 PZ
Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
Loamy Upland 16-20 PZ

I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ??

Thank you
    [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: irregular splits in dataframe

arun kirshna
This post has NOT been accepted by the mailing list yet.
In reply to this post by Sapana Lohani
Hi,
Try this:

dat1 <- readLines(textConnection("Granitic Hills 16-20 PZ
 Loamy Upland 16-20 PZ
 Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
 Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ
 Loamy Upland 16-20 PZ"))

dat3<-strsplit(dat1,"/")
data.frame(do.call(rbind,lapply(dat3,`[`,c(1,2))))
#                            X1                          X2
#1      Granitic Hills 16-20 PZ                        <NA>
#2        Loamy Upland 16-20 PZ                        <NA>
#3  Sandy Loam Upland 12-16 PZ    Sandy Loam, Deep 12-16 PZ
#4       Loamy Upland 12-16 PZ   Sandy Loam Upland 12-16 PZ
#5        Loamy Upland 16-20 PZ                        <NA>
A.K.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Irregular splits in dataframe

Sapana Lohani
In reply to this post by Sapana Lohani
Hello,



Re framing my previous question, my dataframe (Soil Type) looks something like below. I want the code to find "/" first and split the string into 2 or 3 depending on the number of "/". I don't want to specify the number of columns after split. How can I do that ?


Granitic Hills 16-20 PZ
Loamy Upland 16-20 PZ
Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ / Limy Slopes 16-20 PZ

Loamy Upland 16-20 PZ

When there is just one type, I want "NA" in the second / third column. 


Thank you
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Irregular splits in dataframe

David Winsemius

On Aug 21, 2012, at 3:24 PM, Sapana Lohani wrote:

> Hello,
>
>
>
> Re framing my previous question, my dataframe (Soil Type) looks  
> something like below. I want the code to find "/" first and split  
> the string into 2 or 3 depending on the number of "/". I don't want  
> to specify the number of columns after split. How can I do that ?
>
>
> Granitic Hills 16-20 PZ
> Loamy Upland 16-20 PZ
> Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
> Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ / Limy Slopes  
> 16-20 PZ
>
> Loamy Upland 16-20 PZ
>
> When there is just one type, I want "NA" in the second / third column.

  inp <- read.table(text="Granitic Hills 16-20 PZ
  Loamy Upland 16-20 PZ
  Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ
  Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ / Limy Slopes  
16-20 PZ
  Loamy Upland 16-20 PZ", sep="/", fill = TRUE)
  is.na(inp) = inp==""
  inp
#-----------------
                            V1                            
V2                    V3
1     Granitic Hills 16-20 PZ                          
<NA>                  <NA>
2       Loamy Upland 16-20 PZ                          
<NA>                  <NA>
3 Sandy Loam Upland 12-16 PZ     Sandy Loam, Deep 12-16  
PZ                  <NA>
4      Loamy Upland 12-16 PZ   Sandy Loam Upland 12-16 PZ   Limy  
Slopes 16-20 PZ
5       Loamy Upland 16-20 PZ                          
<NA>                  <NA>

--

David Winsemius, MD
Alameda, CA, USA

______________________________________________
[hidden email] 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.
Loading...