简体   繁体   English

从 R 中的数据框中删除“$”

[英]Removing the "$" from a dataframe in R

I have a 10 x ~15,000 data frame with salaries in column 9 and I'm trying to remove the $ from the start of each entry in that column.我有一个 10 x ~15,000 数据框,第 9 列中的薪水是,我试图从该列中每个条目的开头删除 $。

This is the best version of what I have.这是我所拥有的最好的版本。 I am new to R and far more familiar with other languages.我是 R 的新手,对其他语言更加熟悉。 Preferably if there is a way to run an operation on each element of a data frame (like cellfun in Matlab, or a list comprehension in python) that would make this far easier.如果有一种方法可以对数据框的每个元素(如 Matlab 中的 cellfun 或 Python 中的列表推导式)运行一个操作,这将使这更容易。 Based on my debugging attempts it seems like gsub just isn't doing anything, even outside a loop.根据我的调试尝试,即使在循环之外,gsub 似乎也没有做任何事情。 Any suggestions from a more experienced user would be appreciated.来自更有经验的用户的任何建议将不胜感激。 Thanks.谢谢。

bbdat <- read.csv("C:/Users/musta/Downloads/BBs1.csv", header=TRUE, sep=",", dec=".", stringsAsFactors=FALSE)
i <- 0
for (val in bbdat[,9])
{
  i = i+1
  bbdat[i,9]<- gsub("$","",val)
}

The $ is a metacharacter and it implies the end of the string. $是一个元字符,它意味着字符串的结尾。 If we want to evaluate it literally, either use the fixed = TRUE (by default it is FALSE ) or keep it inside square bracket ( "[$]" ) or escape ( \\\\$ ).如果我们想从字面上评估它,要么使用fixed = TRUE (默认情况下它是FALSE )或将其保留在方括号内( "[$]" )或转义( \\\\$ )。 As gsub/sub are vectorized, looping is not required由于gsub/sub是矢量化的,因此不需要循环

bbdat[,9] <- gsub("$", "", bbdat[,9], fixed = TRUE)

If there is only a single instance of $ in each element, use sub ( gsub - global substitution) instead of gsub`如果每个元素中只有一个$实例,请使用sub ( gsub - global substitution) instead of gsub`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM