【发布时间】:2013-11-08 13:05:49
【问题描述】:
我有一个包含
的数据框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 中规范化长数据帧。
编辑
请注意,这只是一个可重复的示例,我的 name 列中有多个名称,并且名称的数量从一行到另一行不等。谢谢。
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")
【问题讨论】:
-
请查看link。一个好的可重复示例将帮助其他人更轻松地解决您的问题。
-
请提供
dput(df)的输出,以便我们可以在我们这边复制df -
您混淆了原子性的含义。要真正让
data.frame成为原子向量,只需使用unlist( data.frame ),但这不会得到你想要的输出。这是一个关于重塑数据的问题。 -
@geektrader dput(df) structure(list(Date = structure(1:2, .Label = c("12/09/2012", "13/09/2012"), class= "因子"), 名称 = 结构(c(2L, 1L), .Label = c("abc\nxyz", "Mahesh\nRahul"), class= "因子"), 分数 = 结构(c(2L, 1L) ), .Label = c("110", "120"), class= "factor")), .Names = c("Date", "name", "score"), row.names = c(NA, -2L), class= "data.frame")
-
下次我会记住如何正确编写可重现的考试,谢谢。
标签: r dataframe normalization