[R] When creating a data frame with data.frame() transforms "integers" into "factors"
Bert Gunter
gunter.berton at gene.com
Sun May 26 02:44:52 CEST 2013
Huh?
> z <- sample(1:10,30,rep=TRUE)
> tbl <- table(z)
> tbl
z
1 2 3 4 5 6 7 8 9 10
4 3 2 6 3 3 2 2 2 3
> data.frame(z)
z
1 5
2 2
3 4
4 1
5 6
6 4
7 10
8 4
9 3
10 8
11 10
12 4
13 3
14 9
15 2
16 2
17 6
18 1
19 4
20 7
21 9
22 10
23 7
24 5
25 5
26 6
27 8
28 1
29 1
30 4
> sapply(data.frame(z),class)
z
"integer"
Your error: you used df['posts'] . You should have used df[,'posts'] .
The former is a data frame. The latter is a vector. Read the
"Introduction to R tutorial" or ?"[" if you don't understand why.
-- Bert
-- Bert
On Sat, May 25, 2013 at 12:36 PM, António Camacho <toinobc at gmail.com> wrote:
> Hello
>
>
> I am novice to R and i was learning how to do a scatter plot with R using
> an example from a website.
>
> My setup is iMac with Mac OS X 10.8.3, with R 3.0.1, default install,
> without additional packages loaded
>
> I created a .csv file in vim with the following content
> userID,user,posts
> 1,user1,581
> 2,user2,281
> 3,user3,196
> 4,user4,150
> 5,user5,282
> 6,user6,184
> 7,user7,90
> 8,user8,74
> 9,user9,45
> 10,user10,20
> 11,user11,3
> 12,user12,1
> 13,user13,345
> 14,user14,123
>
> i imported the file into R using : ' df <- read.csv('file.csv')
> to confirm the data types i did : 'sappily(df, class) '
> that returns "userID" --> "integer" ; "user" ---> "factor" ; "posts" --->
> "integer"
> then i try to create another data frame with the number of posts and its
> frequencies,
> so i did: 'postFreqCount<-data.frame(table(df['posts']))'
> this gives me the postFreqCount data frame with two columns, one called
> 'Var1' that has the number of posts each user did, and another collumn
> 'Freq' with the frequency of each number of posts.
> the problem is that if i do : 'sappily(postFreqCount['Var1'],class)' it
> returns "factor".
> So the data.frame() function transformed a variable that was "integer"
> (posts) to a variable (Var1) that has the same values but is "factor".
> I want to know how to prevent this from happening. How do i keep the values
> from being transformed from "integer" to "factor" ?
>
> Thank you for your help
>
> António
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list