当第一列中的值不为NA时,拆分数据帧 [英] Split dataframe when value in first column is not NA

查看:143
本文介绍了当第一列中的值不为NA时,拆分数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将excel中的数据集导入r-studio。

I have imported a data set looking like this from excel to r-studio

Station Indicator Resp Status
   1        Ca      +      16 
   1        Zn      -      134
   1        Pb      +      3.5
   2        Ca      +      4.6
   3        Zn      -      156

==========

==========

我想以某种方式拆分数据框,当站不是NA时,它将使用站fx1的名称创建一个新的数据帧。
这样我就可以为我的每个电台提供数据框。

I would like to split the dataframe in a way, that when "station" is not NA it would make a new dataframe with the name of the station fx "1". In that way I would have a dataframe for each of my stations.

station 1:
  Ca      +      16 
  Zn      -      134
  Pb      +      3.5

station 2:
  Ca      +      4.6

station 3: 
  Zn      -      156

我已尝试拆分功能

df1=split(df,station=!"NA"])

但它对我来说不起作用

推荐答案

我们可以 split 数据集('df1')与第一列ie

We could split the dataset ('df1') with the first column i.e. Station

 lst <- split(df1[-1], df1$Station)
 names(lst) <- paste0('Station', names(lst))

如果我们在全球环境中需要单独的对象(不推荐)

If we need separate objects in the global environment (not reccommended)

 list2env(lst, envir = .GlobalEnv)

 Station1
 #   Indicator Resp Status
 #1        Ca    +   16.0
 #2        Zn    -  134.0
 #3        Pb    +    3.5

这篇关于当第一列中的值不为NA时,拆分数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆