R 拆分数据帧会产生奇怪的输出
我只是试图根据我的列R 拆分数据帧会产生奇怪的输出,r,list,dataframe,split,R,List,Dataframe,Split,我只是试图根据我的列interference的值将我的数据帧分割成几个数据帧,但是当我尝试这样做时,我得到了一些意外的输出 检查是否确实有一个名为raw的数据帧: print(class(raw)); 屈服 [1] "data.frame" 以下是拆分前的数据帧: position id equation intervention 1 -2 D9E4262D-5B6D-ADB8-D605-B97D6
interference
的值将我的数据帧分割成几个数据帧,但是当我尝试这样做时,我得到了一些意外的输出
检查是否确实有一个名为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);
我明白了:
$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的输出(原始):
编辑
这是我的全部代码,很小
#!/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");
当您将RJSON的输出转换为data.frame时,它正在创建列表的dataframe
这条线是你的问题
#reformats data into dataframe
raw <- as.data.frame(do.call(rbind,raw));
#将数据重新格式化为数据帧
我不能复制这个。请提供来自dput(raw)
的输出。当我像您在这里所做的那样将文本复制并粘贴到终端时,我也得到了这个结果……哦,我明白了,看看raw1的dput。fromJson给了我一个数据帧列表,每个数据帧有一个变量。as.data.frame(do.call(rbind,raw)试图将其转换为正确的数据帧,但现在我有了一个数据帧,其中每一行都是一个列表。有没有办法将其格式化为正确的数据帧?@pandemonimsyndicateraw
#!/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
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
> 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"
#reformats data into dataframe
raw <- as.data.frame(do.call(rbind,raw));
raw2<-data.frame(lapply(raw,unlist))