如何从数据框中拆分所有变量?
How to split all the variables from a dataframe?
我有一个 table 喜欢:
$V1 $V2 $V3
toto|380 peper|728A tomato|I765
toto|458 peper|798A ognion|L965
toto|3L0 ginger|2R8A lemon|I765
我想用“|”分割它最后得到:
$V1 $V2 $V3
toto peper tomato
toto peper ognion
toto ginger lemon
我试过:
split(data, sep="|")
但它不起作用。
你有什么想法吗?
您很可能正在寻找 sub
或 gsub
,或者可能 strsplit
--但绝对不是 split
:
mydf[] <- lapply(mydf, function(x) {
sub("\|.*$", "", x)
})
mydf
## X.V1 X.V2 X.V3
## 1 toto peper tomato
## 2 toto peper ognion
## 3 toto ginger lemon
我有一个 table 喜欢:
$V1 $V2 $V3
toto|380 peper|728A tomato|I765
toto|458 peper|798A ognion|L965
toto|3L0 ginger|2R8A lemon|I765
我想用“|”分割它最后得到:
$V1 $V2 $V3
toto peper tomato
toto peper ognion
toto ginger lemon
我试过:
split(data, sep="|")
但它不起作用。
你有什么想法吗?
您很可能正在寻找 sub
或 gsub
,或者可能 strsplit
--但绝对不是 split
:
mydf[] <- lapply(mydf, function(x) {
sub("\|.*$", "", x)
})
mydf
## X.V1 X.V2 X.V3
## 1 toto peper tomato
## 2 toto peper ognion
## 3 toto ginger lemon