R 更改数据框中的元素

发布于 2024-11-18 21:02:07 字数 669 浏览 2 评论 0原文

正如标题所示,我试图将数据框中的元素从一个字符更改为另一个字符。数据框如下:

g1=c("CC","DD","GG")
g2=c("AA","BB","EE")
g3=c("HH","II","JJ")

df=data.frame(g1,g2,g3)

我希望将元素从字母格式转换为字母/字母格式(例如CC到C/C或AA到A/A)

我知道使用“strsplit”可以在列表上使用。 我还知道我需要以某种方式合并:collapse="/"

我如何能够将 strsplit 函数应用到整个数据帧?

我在想一些类似的事情:

split=function(x)
{
  unlist(paste(strsplit(x,""),collapse="/"))
}

j=as.data.frame(apply(df,1,split))

但它没有给出预期的结果。

更新 - - - - - - - - 显然,以下脚本是有效的:

split=function(x)
{
  paste(unlist(strsplit(x,"")),collapse="/")
}

p=apply(df,c(1,2),split)

如果有更有效或更方便的方法,请随时分享。

I'm trying to, as the title says, change elements from my dataframe from one character to another. The dataframe is as follows:

g1=c("CC","DD","GG")
g2=c("AA","BB","EE")
g3=c("HH","II","JJ")

df=data.frame(g1,g2,g3)

I wish to convert the elements from letterletter format to letter/letter format (e.g. CC to C/C or AA to A/A)

I know using "strsplit" would work on a list.
I also know that I would need to somehow incorporate: collapse="/"

How would I be able to apply the strsplit function to the entire dataframe?

I was thinking something along the lines of:

split=function(x)
{
  unlist(paste(strsplit(x,""),collapse="/"))
}

j=as.data.frame(apply(df,1,split))

but it doesn't give the desired results.

Update----------------
Apparently, the following script works:

split=function(x)
{
  paste(unlist(strsplit(x,"")),collapse="/")
}

p=apply(df,c(1,2),split)

If there is a more efficient or convenient way, please feel free to share.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦醒灬来后我 2024-11-25 21:02:07

我可以想到两种方法来解决这个问题。一种是像您一样使用 strsplit 。您只缺少循环遍历从 strsplit 返回的列表中的每个元素的部分:

Split <- function(x) {
  #unlist(lapply(strsplit(x, ""), paste, collapse="/"))
  sapply(strsplit(x, ""), paste, collapse="/")
}
as.data.frame(lapply(df, Split))

另一种方法是使用 gsub\\B 符号,它匹配不在“单词”开头或结尾的空字符串。

as.data.frame(lapply(df, gsub, pattern="\\B", replacement="/"))

“单词”的构成取决于区域设置和实现,因此这里是另一个使用 gsub 和反向引用的解决方案。

as.data.frame(lapply(df, gsub, pattern="(.)(.)", replacement="\\1/\\2"))

I can think of two ways to approach this. One is using strsplit like you did. You were only missing the portion where you loop over each element in the list returned from strsplit:

Split <- function(x) {
  #unlist(lapply(strsplit(x, ""), paste, collapse="/"))
  sapply(strsplit(x, ""), paste, collapse="/")
}
as.data.frame(lapply(df, Split))

Another approach would be to use gsub and the \\B symbol, which matches the empty string that isn't at the beginning or end of a "word".

as.data.frame(lapply(df, gsub, pattern="\\B", replacement="/"))

What constitutes a "word" depends on locale and implementation, so here's another solution using gsub and back-references.

as.data.frame(lapply(df, gsub, pattern="(.)(.)", replacement="\\1/\\2"))
司马昭之心 2024-11-25 21:02:07

从这样的函数定义开始

insertslash <- function(x) sapply(strsplit(x, ""), function(x) paste(x, collapse="/"))

通过 insertslash(g1) 说服自己它执行了它应该做的事情。

要将其应用到数据帧的所有列,请执行以下操作:

as.data.frame(apply(df, 2, insertslash))

显然,您可以将其滚动到一个令人讨厌的单行代码中:

as.data.frame(apply(df, 2, function(x) sapply(strsplit(x, ""), function(x) paste(x, collapse="/"))))

Start with a function definition like this

insertslash <- function(x) sapply(strsplit(x, ""), function(x) paste(x, collapse="/"))

Convince yourself that it does what it should by insertslash(g1).

To apply it to all columns of the dataframe, do this:

as.data.frame(apply(df, 2, insertslash))

Obviously, you can roll this into one nasty one-liner:

as.data.frame(apply(df, 2, function(x) sapply(strsplit(x, ""), function(x) paste(x, collapse="/"))))
ま昔日黯然 2024-11-25 21:02:07

这是使用 gsub 的一些技巧。对正则表达式了解更多的人应该能够对此进行改进:

mySplit <- function(x)
{
  substr(gsub("","/",x),2,4)
}

as.data.frame(apply(df,2,mySplit))

您最初的解决方案不起作用的原因是您在错误的位置unlist。因此,如果您稍后unlist并使用lapply,事情就会如您所期望的那样工作:

mySplit1 <- function(x)
{
  unlist(lapply(strsplit(x,""),paste,collapse="/"))
}

as.data.frame(apply(df,2,mySplit1))

Here's a bit of a hack using gsub. Someone who knows more about regex ought to be able to improve on this:

mySplit <- function(x)
{
  substr(gsub("","/",x),2,4)
}

as.data.frame(apply(df,2,mySplit))

The reason your original solution wasn't working was because you were unlisting at the wrong spot. So if you unlist later and use lapply things work as you might expect:

mySplit1 <- function(x)
{
  unlist(lapply(strsplit(x,""),paste,collapse="/"))
}

as.data.frame(apply(df,2,mySplit1))
被翻牌 2024-11-25 21:02:07

另一个使用paste()的黑客,绝对不是那么优雅,但它完成了工作。

for (col in 1:ncol(df)){
  df[,col] = paste(substr(df[,col],1,1),"/",substr(df[,col],1,1), sep="")
}

Another hack using paste(), definitely not as elegant but it gets the job done.

for (col in 1:ncol(df)){
  df[,col] = paste(substr(df[,col],1,1),"/",substr(df[,col],1,1), sep="")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文