R 创建TIBLE返回错误:“0”;“如何命名第1列”;
我试图创建一个单列TIBLE,其中每一行都是来自向量的单个单词,但我不确定如何添加列名,因为尚未创建TIBLE 此代码:R 创建TIBLE返回错误:“0”;“如何命名第1列”;,r,R,我试图创建一个单列TIBLE,其中每一行都是来自向量的单个单词,但我不确定如何添加列名,因为尚未创建TIBLE 此代码: as_tibble(strsplit("this is a test" , " ")) 返回错误: Error: Column 1 must be named 如何命名第1列 我试图在的结果中添加名称 strsplit("this is a test" , " ") 但这是一个字符串,因此无法命名 [[1]] [1] “这”是“一个”测试“隔离所创建列表的第一个元素:
as_tibble(strsplit("this is a test" , " "))
返回错误:
Error: Column 1 must be named
如何命名第1列
我试图在的结果中添加名称
strsplit("this is a test" , " ")
但这是一个字符串,因此无法命名
[[1]]
[1] “这”是“一个”测试“隔离所创建列表的第一个元素:
as_tibble(strsplit("this is a test" , " ")[[1]])
或将列表元素命名为:
ls=strsplit("this is a test" , " ")
names(ls)="test_tibble"
as_tibble(ls)
如果需要列名x,我会将strsplit转换为列表:
as_tibble(list(x = (strsplit("this is a test" , " "))[[1]]))
# A tibble: 4 x 1
x
<chr>
1 this
2 is
3 a
4 test
as_tible(list(x=(strsplit(“这是一个测试)”)[[1]]))
#一个tibble:4x1
x
1这个
2是
3A
4试验
您只需执行以下操作:
> strsplit("this is a test" , " ") %>% as_tibble(validate=F)
# A tibble: 4 x 1
``
<chr>
1 this
2 is
3 a
4 test
>strsplit(“这是一个测试,”)%>%as_tible(validate=F)
#一个tibble:4x1
``
1这个
2是
3A
4试验
或者你可以做一些事情,比如:
> strsplit("this is a test" , " ") %>% map(as_tibble) %>% bind_rows
# A tibble: 4 x 1
value
<chr>
1 this
2 is
3 a
4 test
>strsplit(“这是一个测试,”)%%>%map(可存储)%%>%bind\u行
#一个tibble:4x1
价值
1这个
2是
3A
4试验
这是很有用的,因为有时需要一个默认列名来执行更复杂的列表操作