首页 > 其他分享 >In R, how to split/subset a data frame by factors in one column?

In R, how to split/subset a data frame by factors in one column?

时间:2023-11-06 16:02:44浏览次数:38  
标签:subset factors column MN AL Rate State data ID

按照某列的值拆分data.frame

 

My data is like this (for example):

ID  Rate    State
1   24  AL
2   35  MN
3   46  FL
4   34  AL
5   78  MN
6   99  FL

I want to split the data by state and I want to get 3 data sets like below:

data set 1
ID  Rate    State
1   24  AL
4   34  AL
data set 2
ID  Rate    State
2   35  MN
5   78  MN
data set 3
ID  Rate    State
3   46  FL
6   99  FL
split( df , df$State )
$AL
  ID Rate State
1  1   24    AL
4  4   34    AL

$FL
  ID Rate State
3  3   46    FL
6  6   99    FL

$MN
  ID Rate State
2  2   35    MN
5  5   78    MN
mylist <- split( df , df$State ) ; 
mylist[[1]]



标签:subset,factors,column,MN,AL,Rate,State,data,ID
From: https://blog.51cto.com/emanlee/8213339

相关文章

  • P1466 [USACO2.2] 集合 Subset Sums
    P1466USACO2.2集合SubsetSums毫无思路如果不告诉我这题是DP题,我一定会爆搜。看了题解,很妙。居然也能套背包板子。定义F[i][j]为在前\(i\)个数中选择一些数其和为\(j\)的方案总数。显然转移方程F[i][j]=F[i-1][j]+F[i-1][j-i]要么不选当前第\(i\)个数,要么选......
  • 报错 org.springframework.dao.DataIntegrityViolationException: Error attempting t
       原因是持久化层的字段属性 跟数据库的没有对应上,类型不对dao.DataIntegrityViolationException:Errorattemptingtogetcolumn'STATUS'fromresultset.<iftest="record.status!=null">'STATUS'=#{record.status,jdbcType=......
  • 解决MYSQL查询报错 Expression #4 of SELECT list is not in GROUP BY clause and con
    原因:在MySQL5.7.5后,默认开启了ONLY_FULL_GROUP_BY,所以导致了之前的一些SQL无法正常执行,其实,是我们的SQL不规范造成的,因为groupby之后,返回的一些数据是不确定的,所以才会出现这个错误。执行下面的命令后,重启你的代码,就可以了selectversion(),@@sql_mode;SETsql_mode=(SELECTRE......
  • ACCESS ColumnHidden 隐藏字段
    ForEachTEMPInMe.FM刀具项夹表.Form.ControlsIfNotTypeOfTEMPIsLabelThenIfTEMP.Name<>"RecordID"AndTEMP.Name<>"项目"AndTEMP.Name<>"夹位"AndTEMP.Name<>"标准寿命"T......
  • 一键解决json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    json.decoder.JSONDecodeError:Expectingvalue:line1column1(char0)文章目录问题描述解决思路解决方法问题描述json.decoder.JSONDecodeError:Expectingvalue:line1column1(char0)解决思路JSONDecodeError是指在使用json.loads()方法时,解析JSONJSONDecodeError是......
  • Example: Pandas Excel output with column formatting pandas 对excel 列做格式处理
    AnexampleofconvertingaPandasdataframetoanExcelfilewithcolumnformatsusingPandasandXlsxWriter.Itisn’tpossibletoformatanycellsthatalreadyhaveaformatsuchastheindexorheadersoranycellsthatcontaindatesordatetimes.Note:......
  • Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregate
    这个报错的完整信息Expression#3ofSELECTlistisnotinGROUPBYclauseandcontainsnonaggregatedcolumn'jira.ji.ID'whichisnotfunctionallydependentoncolumnsinGROUPBYclause;thisisincompatiblewithsql_mode=only_full_group_by这个说的意......
  • ORA-12899: value too large for column
    Errorsinfile/lbc/lionrdb/app/product/diag/rdbms/cnlionrdb/lionrdb02/trace/lionrdb02_j000_242326.trc:ORA-12012:erroronautoexecuteofjob3964ORA-12008:errorinmaterializedviewrefreshpathORA-12899:valuetoolargeforcolumn"FLUSR"......
  • [909] Remove duplicated rows based on multiple columns in Pandas
    InaPandasDataFrame,youcanremoveduplicatedrowsbasedonmultiplecolumnsusingthedrop_duplicates()method.Here'showyoucandoit:importpandasaspd#SampleDataFramedata={'A':[1,2,3,2,1],'B':[�......
  • Mybatis-Flex核心功能之@Column
    1、是什么?MyBatis-Flex提供了@Column用来对字段进行更多的配置public@interfaceColumn{/***字段名称*/Stringvalue()default"";/***是否忽略该字段,可能只是业务字段,而非数据库对应字段*/booleanignore()defaultfal......