【问题标题】:Splitting dataframe gives strange output拆分数据框会产生奇怪的输出
【发布时间】:2012-07-19 22:03:28
【问题描述】:

我只是想根据我的列intervention 的值将我的数据框拆分为几个数据框,但是当我尝试执行此操作时会得到一些意外的输出。

检查我确实有一个名为 raw 的数据框:

print(class(raw));

产量

[1] "data.frame"

这是拆分前的数据框:

  position                                   id equation      intervention
1       -2 D9E4262D-5B6D-ADB8-D605-B97D63437064      9,5            corral
2        1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B      2,3            corral
3        1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D      1,2         horseshoe
4       -2 76A55530-5A39-6A73-3216-D276EABFA2F6      3,4 test_intervention
5       -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB      3,4 test_intervention

那我用

groups <- split(raw, raw$intervention);

当我打印时

print(groups);

我明白了:

$corral.corral.horseshoe.test_intervention.test_intervention
  position                                   id equation      intervention
1       -2 D9E4262D-5B6D-ADB8-D605-B97D63437064      9,5            corral
2        1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B      2,3            corral
3        1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D      1,2         horseshoe
4       -2 76A55530-5A39-6A73-3216-D276EABFA2F6      3,4 test_intervention
5       -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB      3,4 test_intervention

这看起来不像是按干预分组的数据框列表。还要注意奇怪的线

$corral.corral.horseshoe.test_intervention.test_intervention

编辑

dput 的输出(原始):

structure(list(position = list(-2, 1, 1, -2, -1), id = list("D9E4262D-5B6D-ADB8-D605-B97D63437064",
    "B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B", "85905A69-50F7-AF73-7A51-08B8FDCFAF2D",
    "76A55530-5A39-6A73-3216-D276EABFA2F6", "4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB"),
    equation = list("9,5", "2,3", "1,2", "3,4", "3,4"), intervention = list(
        "corral", "corral", "horseshoe", "test_intervention",
        "test_intervention")), .Names = c("position", "id", "equation",
"intervention"), row.names = c(NA, -5L), class = "data.frame")

编辑 这是我的完整代码,它很小。

#!/usr/local/bin/Rscript --slave
require("rjson", quietly=TRUE);

# First we need to grab the items from the R api, and save them into a data frame
raw = fromJSON(file="http://some/url.com");

#reformats data into dataframe
raw <- as.data.frame(do.call(rbind,raw));
#we need to create a new dataframe formatted according to the needs of catR
groups <- split(raw, raw$intervention);
print(groups); 
#saveRDS(object=fromJSON(file="http://some/url.com"),file="/home/bitnami/IRT_data/core_standard.rda");

我这样运行我的代码:

~/Rscript my_R_file.R

【问题讨论】:

  • 我无法复制这个。请提供dput(raw)的输出。

标签: r list dataframe split


【解决方案1】:
raw1<-read.table(header=T,text="  position                                   id equation      intervention
1       -2 D9E4262D-5B6D-ADB8-D605-B97D63437064      9,5            corral
2        1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B      2,3            corral
3        1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D      1,2         horseshoe
4       -2 76A55530-5A39-6A73-3216-D276EABFA2F6      3,4 test_intervention
5       -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB      3,4 test_intervention")

groups <- split(raw1, raw1$intervention)

> groups
$corral
  position                                   id equation intervention
1       -2 D9E4262D-5B6D-ADB8-D605-B97D63437064      9,5       corral
2        1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B      2,3       corral

$horseshoe
  position                                   id equation intervention
3        1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D      1,2    horseshoe

$test_intervention
  position                                   id equation      intervention
4       -2 76A55530-5A39-6A73-3216-D276EABFA2F6      3,4 test_intervention
5       -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB      3,4 test_intervention

和你不一样。似乎工作正常

raw 是你的数据框,raw1 是 read.table 版本

> str(raw1$intervention)
 Factor w/ 3 levels "corral","horseshoe",..: 1 1 2 3 3
> str(raw$intervention)
List of 5
 $ : chr "corral"
 $ : chr "corral"
 $ : chr "horseshoe"
 $ : chr "test_intervention"
 $ : chr "test_intervention"

当您将输出从 RJSON 转换为 data.frame 时,它​​会创建一个列表数据框

这条线是你的问题

#reformats data into dataframe
raw <- as.data.frame(do.call(rbind,raw));

使用

 raw2<-data.frame(lapply(raw,unlist))

【讨论】:

  • 当我像您在此处所做的那样将文本复制并粘贴到终端时,我也得到了这个......
  • 哦,我明白了,正在查看 raw1 的 dput。所以 fromJson 给了我一个数据框列表,每个数据框都有一个变量。 as.data.frame(do.call(rbind,raw) 尝试将其转换为正确的数据框,但现在我有一个数据框,其中每一行都是一个列表。有没有办法可以将其格式化为正确的数据框?
  • @PandemoniumSyndicate raw &lt;- data.frame(lapply(raw, unlist)) 看起来它会将raw 从其当前状态转换为适当的数据帧。但可能有更好的方法可以完全跳过这种奇怪的状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多