English 中文(简体)
如何改变数据框架的浏览量——以另一个数值取代一个数值
原标题:How to mutate rows of data frame - replacing one value with another
  • 时间:2010-05-12 21:57:49
  •  标签:
  • r

我对我认为是一项基本的R任务感到不安。

在此,我标出的B的样本数据框

Winner Color Size
Tom Yellow Med
Jerry Yellow Lar
Jane Blue Med

在Winner一栏中的项目为因素。

我试图将数据框中的“Tom”改为“Tom Ltd”,我可以这样做。

我在此试图:

Simple way: b$winner[b$winner== Tom ] = as.factor( Tom LLC )

但是,这不符合“无效因素水平,产生新国籍”

随后,我尝试了一条更先进的路线:

name_reset = function (x, y, z) {
if (x$winner == y) {x$winner = z}
}

b = adply(b,1,name_reset, Tom , Tom LLC )

but that failed with "Error in list_to_dataframe(res, attr(.data, "split_labels")) : Results are not equal lengths"

我觉得我没有东西。 一些人是否可以转而我,或就上文提到的守则提出建议? 非常感谢

最佳回答

你们想要做的是通过水平改变价值观。 职等使你能够以一个因素获得标签。 把它放在一个因素上,表明标签,并分配到各个级别上,超出该系数的标签。

一旦你开始履行各级职能,你就可以改变你们想要的价值观。 我认为,也许最容易。

为此:

levels(b$Winner) <- gsub("Tom", "Tom LLC", levels(b$Winner))

- 男性

问题回答

我制定了你的数据框架,然后使用<代码>dput()使数据成为一种格式,使人们可以方便地从网上复制/复制:

b <- structure(list(Winner = c("Tom", "Jerry", "Jane"), Color = c("Yellow", 
"Yellow", "Blue"), Size = c("Med", "Lar", "Med")), .Names = c("Winner", 
"Color", "Size"), row.names = c(NA, -3L), class = "data.frame")

我不肯定你守则中“<代码>的确切含义。 <条码>作为要素将价值观的矢量转换成要素,但对于单一价值来说,这并不真正有意义。 如果家庭主妇是个性病媒,则这项工作:

b$Winner[dat$Winner %in% "Tom"] <- "Tom LLC"

如果Winner是个因素,那么“Tom Ltd”必须成为等级之一,以便你将它插入系数。 如果Winner是个因素,我或许会这样做:

levels(b$Winner) <- c("Tom LLC", "Jerry", "Jane")

这只是告诉R,Winner(即Winner)的可能价值应当替换。 这里的一些先进R用户建议确定你的方位。 作为FALSE的选择,我同意越多使用R。 它非常容易地操纵铺设病媒的平原,然后将其注入必要的因素。





相关问题
How to plot fitted model over observed time series

This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...

REvolution for R

since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...

R - capturing elements of R output into text files

I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...

R statistical package: wrapping GOFrame objects

I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...

Changing the order of dodged bars in ggplot2 barplot

I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...

Strange error when using sparse matrices and glmnet

I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...

Generating non-duplicate combination pairs in R

Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...

Per panel smoothing in ggplot2

I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...

热门标签