3

我有一个数据框包含

df 
Date        name             score
12/09/2012  Mahesh\nRahul    120
13/09/2012  abc\nxyz\nrep         110
...........................

我已经尝试过这个以获得原子

name1=str_split(df[,2],"\n")

但不知道如何再次关联,使数据框标准化的最佳方法是什么,以便我可以得到

 df 
Date        name     score
12/09/2012  Mahesh   120
12/09/2012  Rahul    120
13/09/2012  abc      110
13/09/2012  xyz      110
13/09/2012  rep      110
...........................

任何有助于使 R 中的长数据帧标准化。

编辑

请注意,这只是一个可重复的示例,我的姓名列中有多个姓名,姓名的数量从一行到另一行不等。谢谢。

dput(df) structure(list(Date = structure(1:2, .Label = c("12/09/2012", "13/09/2012 "), class = "factor"), name = structure(c(2L, 1L), .Label = c("abc\nxyz", "Mahesh\nRahul"), class = "factor"), score = structure(c(2L, 1L), .Label = c("110", "120"), class = "factor")), .Names = c("Date", "name", "score"), row.names = c(NA, -2L), class = "data.frame")
4

4 回答 4

5

这是一个 R 基础解决方案

更新

> Names <- strsplit(df$name, "\n")
> n <- sapply(Names, length)
> data.frame(cbind(apply(df[,-2], 2, function(x) rep(x, n)), 
                   name=unlist(Names)), row.names = NULL)[,c(1,3,2)]
        Date   name score
1 12/09/2012 Mahesh   120
2 12/09/2012  Rahul   120
3 13/09/2012    abc   110
4 13/09/2012    xyz   110
5 13/09/2012    rep   110

哪里df是:

> dput(df)
structure(list(Date = c("12/09/2012", "13/09/2012"), name = c("Mahesh\nRahul", 
"abc\nxyz\nrep"), score = c(120, 110)), .Names = c("Date", "name", 
"score"), row.names = c(NA, -2L), class = "data.frame")
于 2013-10-29T11:49:37.733 回答
3

这使用起来相对容易data.table(而且显然很快)。

require( data.table )
dt <- data.table( df )
dt[ , list( name = unlist( strsplit( name , "\n" ) ) ) , by = list( Date , score ) ]
#         Date score   name
#1: 12/09/2012   120 Mahesh
#2: 12/09/2012   120  Rahul
#3: 13/09/2012   110    abc
#4: 13/09/2012   110    xyz

作为注释,我将df其作为以下数据(注意实际数据中出现的character类的类...factor

df <- read.delim( text = "Date    name    score
12/09/2012  'Mahesh\nRahul'   120
13/09/2012  'abc\nxyz'       110" ,
sep = "" , h = TRUE , quote = "\'" , stringsAsFactors = FALSE )
于 2013-10-29T11:55:25.293 回答
2

要添加到替代方案中,您可以使用scan很容易地分隔字符串repcbind获得最终的data.frame

df
#         Date          name score
# 1 12/09/2012 Mahesh\nRahul   120
# 2 13/09/2012 abc\nxyz\nrep   110

scan(text=as.character(df$name), what = "")
# Read 5 items
# [1] "Mahesh" "Rahul"  "abc"    "xyz"    "rep"  

cbind(df[rep(rownames(df), 
             sapply(gregexpr("\n", df$name), length)+1), 
         c("Date", "score")], 
      name = scan(text=as.character(df$name), what = ""))
#           Date score   name
# 1   12/09/2012   120 Mahesh
# 1.1 12/09/2012   120  Rahul
# 2   13/09/2012   110    abc
# 2.1 13/09/2012   110    xyz
# 2.2 13/09/2012   110    rep

read.table也可以拆分连接的列:

read.table(text = as.character(df$name), sep = "\n", header = FALSE)
#       V1
# 1 Mahesh
# 2  Rahul
# 3    abc
# 4    xyz
# 5    rep
于 2013-10-29T13:20:48.180 回答
1

这里已经有很好的答案,但这是使用rleandinverse.rle函数的 base R 的另一种方式。@Jilber 的基本 R 解决方案更优雅,但如果您的字符串有两个以上的名称,这种方式将起作用。

df <- read.table(text='Date        name             score
12/09/2012  "Mahesh\nRahul"    120
13/09/2012  "abc\nxyz\nrep"         110', header=TRUE, stringsAsFactors=FALSE)

ns <- strsplit(df$name, '\n')
result <- lapply(lapply(lapply(df, rle), `[[<-`, 'lengths', sapply(ns, length)), inverse.rle)
transform(data.frame(result), name=unlist(ns))
#         Date   name score
# 1 12/09/2012 Mahesh   120
# 2 12/09/2012  Rahul   120
# 3 13/09/2012    abc   110
# 4 13/09/2012    xyz   110
# 5 13/09/2012    rep   110
于 2013-10-29T12:15:54.293 回答