Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 根据列中的值将数据帧拆分为子数据帧_R - Fatal编程技术网

R 根据列中的值将数据帧拆分为子数据帧

R 根据列中的值将数据帧拆分为子数据帧,r,R,我有一个300列的数据框,我想根据列Millage(MPG)中的值拆分数据框 我想拆分数据帧,这样就可以了 我有一个MPG小于14,14-17,大于17的数据帧 y是我的父数据集,我想用上面指定的值将其拆分为低、中、高数据集 我试图让我们的for循环附加小于13.6的值,然后将矩阵插入一个名为low的单独数据框中 for(i in 1:nrow(y)){ if(y[i,2] <13.6){ low_arrayMPG.append(y[i,2]) low_arrayMod

我有一个300列的数据框,我想根据列Millage(MPG)中的值拆分数据框

我想拆分数据帧,这样就可以了

我有一个MPG小于14,14-17,大于17的数据帧

y是我的父数据集,我想用上面指定的值将其拆分为低、中、高数据集

我试图让我们的for循环附加小于13.6的值,然后将矩阵插入一个名为low的单独数据框中

for(i in 1:nrow(y)){
  if(y[i,2] <13.6){
    low_arrayMPG.append(y[i,2])
    low_arrayModel.append(y[i,1])
    low_arrayOrigin.append(y[i,3])

  }

}
for(1中的i:nrow(y)){

如果(y[i,2],我认为您可以将数据帧(
df
)子集如下:

df_low    <- df[df$MPR < 14, ]
df_medium <- df[df$MPR >= 14 & df$MPR <= 17, ]
df_high   <- df[df$MPR > 17, ]

df\u low我认为您可以将数据帧(
df
)子集如下:

df_low    <- df[df$MPR < 14, ]
df_medium <- df[df$MPR >= 14 & df$MPR <= 17, ]
df_high   <- df[df$MPR > 17, ]

df_low我们可以使用
findInterval
创建一个分组变量,用于
split
将数据集划分为
data.frame的
列表

lst <- split(df1, findInterval(df1$MPG, c(14, 17), rightmost.closed = TRUE))

lst我们可以使用
findInterval
split
创建一个分组变量,将数据集划分为
data.frame的
列表

lst <- split(df1, findInterval(df1$MPG, c(14, 17), rightmost.closed = TRUE))

lst也许你也会喜欢这些:

split(df1,(df1$MPG>=14)+(df1$MPG>17))
# $`1`
# Model MPG Origin
# 2          buick skylark 320  15     US
# 4              amc rebel sst  16     US
# 5                ford torino  17     US
# 6           ford galaxie 500  15     US
# 7           chevrolet impala  14     US
# 8          plymouth fury iii  14     US
# 9           pontiac catalina  14     US
# 10        amc ambassador dpl  15     US
# 11       dodge challenger se  15     US
# 
# $`2`
# Model MPG Origin
# 1 chevrolet chevelle malibu  18     US
# 3        plymouth satellite  18     US


library(dplyr)
library(tidyr)
df1 %>% group_by(spl = (MPG>=14) + (MPG>17)) %>% nest
# # A tibble: 2 x 2
#       spl             data
#     <int>           <list>
#   1     2 <tibble [2 x 3]>
#   2     1 <tibble [9 x 3]>
split(df1,(df1$MPG>=14)+(df1$MPG>17))
# $`1`
#模型MPG原点
#2别克云雀320 15美国
#4 amc叛军sst 16美国
#5福特都灵17美国
#6福特galaxie 500 15美国
#7雪佛兰黑斑羚14美国
#8普利茅斯愤怒iii 14美国
#9庞蒂亚克卡塔利纳14美国
#10 amc大使dpl 15美国
#11道奇挑战者se 15美国
# 
# $`2`
#模型MPG原点
#1雪佛兰chevelle malibu 18美国
#3普利茅斯卫星18美国
图书馆(dplyr)
图书馆(tidyr)
df1%>%分组依据(spl=(MPG>=14)+(MPG>17))%>%nest
##tibble:2x2
#spl数据
#                
#   1     2 
#   2     1 
数据

df1 <- read.table(text="                      Model              MPG     Origin
           1              'chevrolet chevelle malibu' 18.0     US
                  2              '        buick skylark 320' 15.0     US
                  3              '       plymouth satellite' 18.0     US
                  4              '            amc rebel sst' 16.0     US
                  5              '              ford torino' 17.0     US
                  6              '         ford galaxie 500' 15.0     US
                  7              '         chevrolet impala' 14.0     US
                  8              '        plymouth fury iii' 14.0     US
                  9              '         pontiac catalina' 14.0     US
                  10             '       amc ambassador dpl' 15.0     US
                  11             '      dodge challenger se' 15.0     US",header=T,stringsAsFactors=F)

df1也许您也会喜欢这些:

split(df1,(df1$MPG>=14)+(df1$MPG>17))
# $`1`
# Model MPG Origin
# 2          buick skylark 320  15     US
# 4              amc rebel sst  16     US
# 5                ford torino  17     US
# 6           ford galaxie 500  15     US
# 7           chevrolet impala  14     US
# 8          plymouth fury iii  14     US
# 9           pontiac catalina  14     US
# 10        amc ambassador dpl  15     US
# 11       dodge challenger se  15     US
# 
# $`2`
# Model MPG Origin
# 1 chevrolet chevelle malibu  18     US
# 3        plymouth satellite  18     US


library(dplyr)
library(tidyr)
df1 %>% group_by(spl = (MPG>=14) + (MPG>17)) %>% nest
# # A tibble: 2 x 2
#       spl             data
#     <int>           <list>
#   1     2 <tibble [2 x 3]>
#   2     1 <tibble [9 x 3]>
split(df1,(df1$MPG>=14)+(df1$MPG>17))
# $`1`
#模型MPG原点
#2别克云雀320 15美国
#4 amc叛军sst 16美国
#5福特都灵17美国
#6福特galaxie 500 15美国
#7雪佛兰黑斑羚14美国
#8普利茅斯愤怒iii 14美国
#9庞蒂亚克卡塔利纳14美国
#10 amc大使dpl 15美国
#11道奇挑战者se 15美国
# 
# $`2`
#模型MPG原点
#1雪佛兰chevelle malibu 18美国
#3普利茅斯卫星18美国
图书馆(dplyr)
图书馆(tidyr)
df1%>%分组依据(spl=(MPG>=14)+(MPG>17))%>%nest
##tibble:2x2
#spl数据
#                
#   1     2 
#   2     1 
数据

df1 <- read.table(text="                      Model              MPG     Origin
           1              'chevrolet chevelle malibu' 18.0     US
                  2              '        buick skylark 320' 15.0     US
                  3              '       plymouth satellite' 18.0     US
                  4              '            amc rebel sst' 16.0     US
                  5              '              ford torino' 17.0     US
                  6              '         ford galaxie 500' 15.0     US
                  7              '         chevrolet impala' 14.0     US
                  8              '        plymouth fury iii' 14.0     US
                  9              '         pontiac catalina' 14.0     US
                  10             '       amc ambassador dpl' 15.0     US
                  11             '      dodge challenger se' 15.0     US",header=T,stringsAsFactors=F)
df1