从向量创建一系列向量 [英] Creating a series of vectors from a vector

查看:130
本文介绍了从向量创建一系列向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的两个向量数据框(长度= 30),看起来像这样:

 > mDF 
Param1 w.IL.L
1 AuZgFw 0.5
2 AuZfFw 2
3 AuZgVw 74.3
4 AuZfVw 20.52
5 AuTgIL 80.9
6 AuTfIL 193.3
7 AuCgFL 0.2
8 ...

我会喜欢使用每个行来形成30个单值数字向量,其矢量名称取自 mDF $ Param1 ,以便:

 > AuZgFw 
[1] 0.5


$ b $我试过熔化和铸造,但我怀疑可能会有一个更简单的方法?

解决方案

最简单/最短的方法是应用 在行上分配

  mDF<  -  read.table(textConnection $ b Param1 w.IL.L 
1 AuZgFw 0.5
2 AuZfFw 2
3 AuZgVw 74.3
4 AuZfVw 20.52
5 AuTgIL 80.9
6 AuTfIL 193.3
7 AuCgFL 0.2
),header = T,stringsAsFactors = F)
invisible(apply(mDF,1,function(x)assign(x [[1]],as.numeric (x [[2]]),envir = .GlobalEnv))

数据帧的列与来自字符串的列。 隐形只能抑制的输出应用

编辑:您也可以使用 mapply 以避免对字符串进行隐写:



invisible(mapply(function(x ,y)assign(x,y,envir = .GlobalEnv),mDF $ Param1,mDF $ w.IL.L))


I have a simple two vector dataframe (length=30) that looks something like this:

> mDF
    Param1 w.IL.L
1   AuZgFw    0.5
2   AuZfFw      2
3   AuZgVw   74.3
4   AuZfVw  20.52
5   AuTgIL   80.9
6   AuTfIL  193.3
7   AuCgFL    0.2
8   ...

I'd like to use each of the rows to form 30 single value numeric vectors with the name of the vector taken from mDF$Param1, so that:

> AuZgFw       
[1] 0.5     

etc

I've tried melting and casting, but I suspect there may be an easier way?

解决方案

The simplest/shortest way is to apply assign over rows:

mDF <- read.table(textConnection("
Param1 w.IL.L
1   AuZgFw    0.5
2   AuZfFw      2
3   AuZgVw   74.3
4   AuZfVw  20.52
5   AuTgIL   80.9
6   AuTfIL  193.3
7   AuCgFL    0.2
"),header=T,stringsAsFactors=F)
invisible(apply(mDF,1,function(x)assign(x[[1]],as.numeric(x[[2]]),envir = .GlobalEnv)))

This involves converting the second column of the data frame to and from a string. invisible is there only to suppress the output of apply.
EDIT: You can also use mapply to avoid coersion to/from strings:

invisible(mapply(function(x,y)assign(x,y,envir=.GlobalEnv),mDF$Param1,mDF$w.IL.L))

这篇关于从向量创建一系列向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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