- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
df name value 1 cat_name Bart 2 c-6ren">
我有一个包含两列的数据框 df
,一列包含名称,第二列包含可以是字符串或 double 值的值,例如
> df
name value
1 cat_name Bart
2 cat_age 5
3 dog_name Fred
4 dog_age 9
5 total_pet 2
I'd like to convert
df
into alist
of named objects so I can calllist$cat_name
and get back a string"Bart"
orlist$bird_age
and get back1
as a numeric.
我试过了
> list <- split(df[, 2], df[, 1])
> list
$cat_age
[1] 5
Levels: 2 5 9 Bart Fred
$cat_name
[1] Bart
Levels: 2 5 9 Bart Fred
$dog_age
[1] 9
Levels: 2 5 9 Bart Fred
$dog_name
[1] Fred
Levels: 2 5 9 Bart Fred
$total_pet
[1] 2
Levels: 2 5 9 Bart Fred
将df
转换为因素 的列表
。这几乎是我想要的,因为 $
运算符工作正常。但是,我并不真正习惯使用因子,我想知道是否还有其他可用的dataframe-to-list 转换。烦人的部分是为了处理字符串和数字,我们必须将因子转换回那些类型
> as.character(list$cat_name)
[1] "Bart"
> as.numeric(as.character(list$total_pet))
[1] 3
在注意到 df[, 1]
和 df[, 2]
实际上是因素之后,我尝试使用
> list <- split(as.character(df[, 2]), df[, 1])
> list
$cat_age
[1] "5"
$cat_name
[1] "Bart"
$dog_age
[1] "9"
$dog_name
[1] "Fred"
$total_pet
[1] "2"
这几乎解决了问题,只是数字是稍后要转换的字符。我也尝试过使用 hash
对象
> h <- hash(as.vector(df[, 1]), as.vector(df[, 2]))
> l = as.list(h)
> l
$dog_age
[1] "9"
$dog_name
[1] "Fred"
$cat_age
[1] "5"
$total_pet
[1] "2"
$cat_name
[1] "Bart"
但我得到了相同的结果。
有人有什么建议吗?我是否遗漏了一些明显的东西?
坦克:)
最佳答案
我们可以使用 type.convert
library(purrr)
map(list, type.convert, as.is = TRUE)
#$cat_age
#[1] 5
#$cat_name
#[1] "Bart"
#$dog_age
#[1] 9
#$dog_name
#[1] "Fred"
#$total_pet
#[1] 2
因为这可以通过并行实现来提高效率,一种选择是 furrr
中的 future_map
library(furrr)
plan(multiprocess)
future_map(list, type.convert, as.is = TRUE)
关于R数据帧到 "dictionary"避免因素列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49883809/
The proper divisors of a positive integer, n, are all the positive integers that divide n evenly oth
我有这个命令行 $ sudo find /etc/grub.d | sort | tail -n 1 | xargs sudo cat | wc 我想用一个 sudo 命令执行 $ sudo --so
选项大小策略和拉伸(stretch)因子如何影响小部件的大小? 下图显示了三个不同排列的窗口的预览。对于所有三个窗口 (W1-W3),右侧的小部件是一个 QFrame 小部件,其水平和垂直大小策略设置
每次当我必须重新编码一组变量时,我都会想到 SPSS 重新编码功能。我必须承认这很简单。有一个类似的recode函数在 car包,它可以解决问题,但让我们假设我想用 factor 完成任务. 我有 d
这个问题在这里已经有了答案: Template issue causes linker error (C++) [duplicate] (6 个答案) 关闭 9 年前。 我的问题查了没用所以特地来问
我想使用 Eigen 来计算稀疏矩阵的 cholesky 分解。但是,结果不正确,我找不到原因。我如何获得正确答案? Eigen 中是否实现了特殊例程,利用稀疏矩阵的结构来提高性能(例如,对于下例中的
我正在尝试使 angularjs 应用程序在配置( http://12factor.net/config )方面符合 12 因素。 它应该取决于环境,我不应该看到 development 字样, te
我在我的项目中使用 Soil,我在我的包含目录中添加了 soil,在我的预编译头文件中我包含了“Soil.h”。对于我预编译头中的库,我添加了这个: #pragma comment(lib,"SOIL
在我的 Web 应用程序中,我将所有最终用户的日期信息以 UTC 格式存储在数据库中,在向他们显示之前,只需将 UTC 日期转换为他们选择的时区。 我正在使用此方法将本地时间转换为 UTC 时间(在存
我的申请是 Piwik Server从放置在数百个网站上的跟踪代码接收传入的跟踪数据。当这些跟踪请求进入时,大部分工作负载是每秒向数据库写入数百次。我使用的是带有 JDBC 和 Hibernate 的
我有一个非常简单的 GWT 应用程序,它收集一些数据并在用户单击“提交”时提供确认对话框。我创建了一个 com.google.gwt.user.client.ui.DialogBox,填充它,然后调用
我正在使用 Delphi(2009 年,没关系)和 IBX,并且我正在尝试执行简单的代码: TestSQL.ExecQuery; 在此代码之前,我已检查(也可以在调试器监视中看到)TestSQL.Tr
许多线性代数例程都将常量(例如 alpha 和 beta)作为参数。例如cublas?GEMM执行以下操作: C := alpha*op( A )op( B ) + betaC 假设我将 beta 设
我是一名优秀的程序员,十分优秀!