Boris -
Boy, do I feel dumb - that’s exactly what I wanted. I’ve tried this every way
I can think of without assigning the result to the original name of the data
frame. I was trying to assign the result to a variable (test$place).
Can u pls explain to me why assigning the result to the new
I guess this thread has gone on long enough, but I haven't seen anyone
yet suggest what to me seems like the obvious thing if you want to do
this with mutate, namely
testdata <- mutate(testdata, place = as.factor(substr(subject, 1, 3)))
Best,
Ista
On Fri, Mar 4, 2016 at 10:45 PM, Boris Steipe w
You mean this?
test$place <- factor(test$place)
You can create a new column in a data frame by assigning something to it. E.g.
test$pollywog <- 1:6
... creates that column in "test".
But factor(test$place) was empty, because no such column previously existed,
like:
R > factor(test$barbapap
LOL you still need to assign it though:
test <- mutate(test, place = factor(substr(test$subject,1,3)))
str(test)
'data.frame': 6 obs. of 7 variables:
$ subject: Factor w/ 6 levels "001-002","002-003",..: 1 2 3 4 5 6
$ group : Factor w/ 2 levels "boys","girls": 1 1 1 2 2 2
$ wk1: int
Here’s where I’m stumped -
when I call mutate(test, place = substr(test$subject, 1,3)) to create a place
variable, I get this, with place as a character variable.
subject group wk1 wk2 wk3 wk4 place
(fctr) (fctr) (int) (int) (int) (int) (chr)
1 001-002 boys 2 3 4
Here’s the dataset I’m working with, called test -
subject group wk1 wk2 wk3 wk4 place
001-002 boys2 3 4 5
002-003 boys7 6 5 4
003-004 boys9 4 6 1
004-005 girls 5 7 8 9
This is not a problem with factor or as.factor, this is a problem with your use
of the dplyr package or with bugs in that package.
Please make a reproducible example, making sure to use the dput function to
create the code that initializes the data so we can run your code. Read the
Posting Gui
You're not saving the result of mutate(). You're just printing it to the screen.
Try instead:
test <- mutate(testdata, place = substr(testdata$subject, 1,3))
test$place <- as.factor(test$place) # or factor() if you'd rather
This is why we ask for reproducible examples with data and code.
Look thr
I much prefer the factor function over the as.factor function for converting
character to factor, since you can set the levels in the order you want them to
be.
--
Sent from my phone. Please excuse my brevity.
On March 4, 2016 10:07:27 AM PST, Sarah Goslee wrote:
>As everyone has been telling
As everyone has been telling you, as.factor().
If you like the mutate approach, you can call as.factor(test$subject)
to convert it.
Here's a one-liner with reproducible data.
testdata <- structure(list(subject = structure(1:6, .Label = c("001-002",
"002-003", "003-004", "004-005", "005-006", "00
Hi Ken,
You do that with as.factor(), as has already been suggested. You'll need to
provide a reproducible example to show us what's going wrong. Using fake
data is fine, we just need to see some data that look like yours and the
code you're using.
Sarah
On Fri, Mar 4, 2016 at 11:57 AM, KMNanus
Let me see if I can ask the question more clearly - I am trying to extract a
section of a hyphenated factor. For example, 001-004 is one observation of
test$ken, which is a factor, and I want to set up a new factor variable called
place that would have 001 as an observation. If I call mutate(pla
On 03/03/2016 02:13 PM, KMNanus wrote:
When I do that,
When you do what exactly?
It's impossible for anyone here to know what you're doing if you
don't show the code.
I get "Error in `$<-.data.frame`(`*tmp*`, "site", value
= integer(0)) :
replacement has 0 rows, data has 6”
The data fram
When I did that, I got -
"Error in `$<-.data.frame`(`*tmp*`, "site", value = integer(0)) :
replacement has 0 rows, data has 6”
The data frame has 6 rows.
Ken
kmna...@gmail.com
914-450-0816 (tel)
347-730-4813 (fax)
> On Mar 3, 2016, at 4:14 PM, Ista Zahn wrote:
>
> Like this?
>
> x <-
When I do that, I get "Error in `$<-.data.frame`(`*tmp*`, "site", value =
integer(0)) :
replacement has 0 rows, data has 6”
The data frame has 6 rows.
Ken
kmna...@gmail.com
914-450-0816 (tel)
347-730-4813 (fax)
> On Mar 3, 2016, at 4:52 PM, Hervé Pagès wrote:
>
> Hi,
>
> On 03/03/2016 1
Hi,
On 03/03/2016 12:18 PM, KMNanus wrote:
I have a factor variable that is 6 digits and hyphenated. For example, 001-014.
I need to extract the first 3 digits to a new variable using mutate in dplyr -
in this case 001 - but can’t find a function to do it.
substr will do this for character s
Like this?
x <- factor("001-014")
y <- substr(as.character(x), 1, 3)
Best,
Ista
On Thu, Mar 3, 2016 at 3:18 PM, KMNanus wrote:
> I have a factor variable that is 6 digits and hyphenated. For example,
> 001-014.
>
> I need to extract the first 3 digits to a new variable using mutate in dply
I have a factor variable that is 6 digits and hyphenated. For example, 001-014.
I need to extract the first 3 digits to a new variable using mutate in dplyr -
in this case 001 - but can’t find a function to do it.
substr will do this for character strings, but I need the variable to remain as
18 matches
Mail list logo