0

こんにちは、管理が難しい状況です。次のような data.frame があります。

General_name
     a 
     b
     c 
     d
     m
     n

そして、次のような別の data.frame :

First_names_list     a=34;b=4   
Second_names_list    d=2;m=98;n=32    
Third_names_list     c=1;d=12;m=0.1

最初の data.frame の各要素を、2 番目の data.frame[,2] の = の前の各要素と一致させて、最終的に次のテーブルを取得する必要があります。

Names                 a     b     c      d      m      n
First_names_list     34     4     NA    NA     NA     NA        
Second_names_list    NA     NA    NA    2      98     32     
Third_names_list     NA     NA    1     12     0.1    NA      

なにか提案を?私には難しすぎるようです。

一番

E.

4

4 に答える 4

2

オプション1

dcast「reshape2」とconcat.split私の「splitstackshape」パッケージから使用する1つのアプローチを次に示します。

library(splitstackshape)
## The following can also be done in 2 steps. The basic idea is to split 
##   the values into a semi-long form for `dcast` to be able to use. So, 
##   I've split first on the semicolon, and made the data into a long form 
##   at the same time, then I've split on =, but kept it wide that time.
out <- concat.split(concat.split.multiple(df, "V2", ";", "long"),
                    "V2", "=", drop = TRUE)
out
#                  V1 time V2_1 V2_2
# 1  First_names_list    1    a 34.0
# 2 Second_names_list    1    d  2.0
# 3  Third_names_list    1    c  1.0
# 4  First_names_list    2    b  4.0
# 5 Second_names_list    2    m 98.0
# 6  Third_names_list    2    d 12.0
# 7  First_names_list    3 <NA>   NA
# 8 Second_names_list    3    n 32.0
# 9  Third_names_list    3    m  0.1

library(reshape2)
dcast(out[complete.cases(out), ], V1 ~ V2_1, value.var="V2_2")
#                  V1  a  b  c  d    m  n
# 1  First_names_list 34  4 NA NA   NA NA
# 2 Second_names_list NA NA NA  2 98.0 32
# 3  Third_names_list NA NA  1 12  0.1 NA

オプション 2

のより新しいバージョンを使用した別のオプションを次に示しますdata.table。この概念は、上記のアプローチと非常によく似ています。

library(data.table)
library(reshape2)
packageVersion("data.table")
# [1] ‘1.8.11’

dt <- data.table(df)
S1 <- dt[, list(X = unlist(strsplit(as.character(V2), ";"))), by = V1]
S1[,  c("A", "B") := do.call(rbind.data.frame, strsplit(X, "="))]
S1
#                   V1     X A   B
# 1:  First_names_list  a=34 a  34
# 2:  First_names_list   b=4 b   4
# 3: Second_names_list   d=2 d   2
# 4: Second_names_list  m=98 m  98
# 5: Second_names_list  n=32 n  32
# 6:  Third_names_list   c=1 c   1
# 7:  Third_names_list  d=12 d  12
# 8:  Third_names_list m=0.1 m 0.1
dcast.data.table(S1, V1 ~ A, value.var="B")
#                   V1  a  b  c  d   m  n
# 1:  First_names_list 34  4 NA NA  NA NA
# 2: Second_names_list NA NA NA  2  98 32
# 3:  Third_names_list NA NA  1 12 0.1 NA

上記のオプションは両方とも、次のものから始めることを前提としています。

df <- structure(list(V1 = c("First_names_list", "Second_names_list", 
    "Third_names_list"), V2 = c("a=34;b=4", "d=2;m=98;n=32", 
    "c=1;d=12;m=0.1")), .Names = c("V1", "V2"), class = "data.frame", 
    row.names = c(NA, -3L))
于 2013-11-01T17:01:33.447 に答える
1

applyapply内で使用するソリューションは次のとおりです。

#Data frame 1
df1 <- read.table(text=
"General_name
a 
b
c 
d
m
n", header=T, as.is=T)

#Data frame 2
df2 <- read.table(text=
"col1     col2
First_names_list     a=34;b=4   
Second_names_list    d=2;m=98;n=32    
Third_names_list     c=1;d=12;m=0.1", header=T, as.is=T)

#make lists for each row, sep by ";"
df2split <- strsplit(df2$col2,split=";") 

#result
t(
  sapply(seq(1:nrow(df2)),function(c){
    x <- df2split[[c]]
    sapply(df1$General_name,function(n){
      t <- gsub(paste0(n,"="),"",x[grepl(n,x)])
      ifelse(length(t)==0,NA,as.numeric(t))
      })
    })
  )
于 2013-11-01T17:10:41.050 に答える
0

これは少し回りくどい方法だと思うので、より良い解決策も楽しみにしています。しかし、これは機能します。

library(data.table)
library(reshape2)

#creating datasets
dt <- data.table(read.csv(textConnection('
"First_names_list","a=34;b=4"
"Second_names_list","d=2;m=98;n=32"
"Third_names_list","c=1;d=12;m=0.1"
'),header = FALSE))

General_name = c('a','b','c','d','m','n')
TotalBreakup <- data.table(
V1 = General_name
)

# Fixing datatypes
TotalBreakup <- TotalBreakup[,lapply(.SD,as.character)]
dt <- dt[,lapply(.SD,as.character)]


# looping through each row and calculating breakdown
for(i in 1:nrow(dt))
{
  # the next two statements are the workhorse of this code. Run each part of these statements step by step to see 
  dtlist <- strsplit(unlist(strsplit(dt[i,V2],";")),"=")
  breakup <- data.table(
    t(
      matrix(
        unlist(
          strsplit(
            unlist(
              strsplit(
                dt[i,V2],
                ";"
                )
              ),
            "="
            )
          ),
        nrow = 2
        )
      )
    )

  # fixing datatypes again
  breakup <- breakup[,lapply(.SD,as.character)]
  #appending to master dataset
  TotalBreakup <- merge(TotalBreakup, breakup, by = "V1", all.x = TRUE)
}

#formatting results
setnames(TotalBreakup,c("Names",dt[,V1]))
TotalBreakup <- acast(melt(TotalBreakup,id.vars = "Names"),variable~Names)

出力 -

> TotalBreakup
                  a    b   c   d    m     n   
First_names_list  "34" "4" NA  NA   NA    NA  
Second_names_list NA   NA  NA  "2"  "98"  "32"
Third_names_list  NA   NA  "1" "12" "0.1" NA
于 2013-11-01T16:45:19.147 に答える
0

方法はこれです:

#the second dataframe you provided
DF2 <- read.table(text = '

   First_names_list     a=34;b=4   
   Second_names_list    d=2;m=98;n=32    
   Third_names_list     c=1;d=12;m=0.1

', header = F, stringsAsFactors = F)

#empty dataframe
DF <- structure(list(a = c(NA, NA, NA), b = c(NA, NA, NA), c = c(NA, 
NA, NA), d = c(NA, NA, NA), m = c(NA, NA, NA), n = c(NA, NA, 
NA)), .Names = c("a", "b", "c", "d", "m", "n"), row.names = c("First_names_list", 
"Second_names_list", "Third_names_list"), class = "data.frame")
DF
#                   a  b  c  d  m  n
#First_names_list  NA NA NA NA NA NA
#Second_names_list NA NA NA NA NA NA
#Third_names_list  NA NA NA NA NA NA

#fill the dataframe
myls <- strsplit(DF2$V2, split = ";")
for(i in 1:length(myls))
{
 sapply(myls[[i]], 
      function(x) { res <- unlist(strsplit(x, "=")) ; DF[i,res[1]] <<- res[2] })
}

DF
#                     a    b    c    d    m    n
#First_names_list    34    4 <NA> <NA> <NA> <NA>
#Second_names_list <NA> <NA> <NA>    2   98   32
#Third_names_list  <NA> <NA>    1   12  0.1 <NA>
于 2013-11-01T16:58:13.607 に答える