Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:56:16+00:00 2026-06-09T14:56:16+00:00

Dear StackOverflow community, I have a dataset from my university projects I am trying

  • 0

Dear StackOverflow community,

I have a dataset from my university projects I am trying to parse and run some calculations on. It looks similar to:

Month,1,2,3,3,4,4,5,6,7
x.1,0,0,0,0,0,0,0,0,0
x.2,0,0,0,0,0,0,0,0,0
x.3,0,0,0,6,5,5,,,15
x.4,0,0,0,7,7,,,,15
x.5,1,1,1,11,7,5,,,0
x.6,1,1,1,14,6,,,,0
x.7,1,1,1,17,5,,,,15
x.8,1,1,1,21,4,,,,15
x.9,0,0,0,1,1,1,1,1,0
x.10,0,0,0,1,1,1,1,1,0
x.11,1,0,0,1,1,1,1,1,0
x.12,0,0,0,0,0,0,0,0,1
x.13,0,0,0,0,0,0,0,0,0
x.14,0,1,0,0,0,0,0,0,0
x.20,orchid,,,orchid,rose,orchid,orchid,orchid,
x.23,0,0,0,1,1,1,1,1,1
x.24,,,,,buttercup,buttercup,buttercup,buttercup,lilac
x.25,0,0,0,1,1,0,1,1,1
x.26,,,,17,,,,,15
x.27,,,,999,,,,,15

I try to then import it like so:

data <- read.csv("~/data_munging/data.csv", header=F)
my_matrix <- as.matrix(data)

The issue here is that the dataset’s first column is actually the names of the variables, and as.matrix() does not read it as row (variable) names.

(There are also holes in some of the data, but that I will leave for another question).

I am new to R and am wondering What am I Doing Wrong™?

Update:
As per Justin’s comments, here is how import the dataset and the str() it produces:

> sample_data <- read.csv("~/data_munging/sample_data.csv", header=F)
> str(sample_data)
'data.frame':   28 obs. of  10 variables:
 $ V1 : Factor w/ 28 levels "Month","x.1","x.10",..: 1 2 13 22 23 24 25 26 27 28 ...
 $ V2 : Factor w/ 4 levels "","0","1","orchid": 3 2 2 2 2 3 3 3 3 2 ...
 $ V3 : int  2 0 0 0 0 1 1 1 1 0 ...
 $ V4 : int  3 0 0 0 0 1 1 1 1 0 ...
 $ V5 : Factor w/ 12 levels "","0","1","11",..: 8 2 2 9 10 4 5 6 7 3 ...
 $ V6 : Factor w/ 9 levels "","0","1","4",..: 4 2 2 5 7 7 6 5 4 3 ...
 $ V7 : Factor w/ 7 levels "","0","1","4",..: 4 2 2 5 1 5 1 1 1 3 ...
 $ V8 : Factor w/ 6 levels "","0","1","5",..: 4 2 2 1 1 1 1 1 1 3 ...
 $ V9 : Factor w/ 6 levels "","0","1","6",..: 4 2 2 1 1 1 1 1 1 3 ...
 $ V10: Factor w/ 6 levels "","0","1","15",..: 5 2 2 4 4 2 2 4 4 2 ...

The reason I believe it should be a matrix is because this way it reads the Month as a factor and its levels are the row names instead of moths (month of year).

Update 2: Now with the original dataset in CSV.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:56:17+00:00Added an answer on June 9, 2026 at 2:56 pm

    There is a transpose method for matrices and dataframes which returns a matrix.:

    tdat <- t( read.table(text="Month,1,2,3,3,4,4,5,6,7
     x.1,0,0,0,0,0,0,0,0,0
     x.2,0,0,0,0,0,0,0,0,0
     x.3,0,0,0,6,5,5,,,15
     x.4,0,0,0,7,7,,,,15
     x.5,1,1,1,11,7,5,,,0
     x.6,1,1,1,14,6,,,,0
     x.7,1,1,1,17,5,,,,15
     x.8,1,1,1,21,4,,,,15
     x.9,0,0,0,1,1,1,1,1,0
     x.10,0,0,0,1,1,1,1,1,0
     x.11,1,0,0,1,1,1,1,1,0
     x.12,0,0,0,0,0,0,0,0,1
     x.13,0,0,0,0,0,0,0,0,0
     x.14,0,1,0,0,0,0,0,0,0
     x.20,orchid,,,orchid,rose,orchid,orchid,orchid,
     x.23,0,0,0,1,1,1,1,1,1
     x.24,,,,,buttercup,buttercup,buttercup,buttercup,lilac
     x.25,0,0,0,1,1,0,1,1,1
     x.26,,,,17,,,,,15
     x.27,,,,999,,,,,15", sep=",", header=FALSE, as.is=TRUE) )
     # It might not be immediately obvious that the transpose function converts to matrix
     newdat <- tdat[-1, ]
     colnames(newdat) <- dat[1,]
     newdat <- as.data.frame(newdat)   
    # when converted back , everything is factors. Will need to convert to get numeric
      newdat[ , -grep("20|24", names(newdat) ) ] <- 
                        lapply(newdat[ , -grep("20|24", names(newdat) )], 
                                 function(x) as.numeric( as.character(x) ))
    # Need to use grep to convert character-names to numeric so can use negative indexing
    # and used the redundant `as.numeric(as.character(x))` to illustrate good practice.
    

    Resulting in:

    > newdat
        Month x.1 x.2 x.3 x.4 x.5 x.6 x.7 x.8 x.9 x.10 x.11 x.12 x.13 x.14   x.20 x.23      x.24 x.25 x.26 x.27
    V2      3   2   2   3   3   4   4   3   3   2    2    3    2    2    3 orchid    2              2    1    1
    V3      1   1   1   2   2   2   2   2   2   1    1    1    1    1    2   <NA>    1      <NA>    1   NA   NA
    V4      2   1   1   2   2   2   2   2   2   1    1    1    1    1    1   <NA>    1      <NA>    1   NA   NA
    V5      4   2   2   6   5   5   5   5   5   3    3    3    2    2    3 orchid    3              3    3    3
    V6      5   2   2   5   5   7   6   6   6   3    3    3    2    2    3   rose    3 buttercup    3    1    1
    V7      5   2   2   5   1   6   1   1   1   3    3    3    2    2    3 orchid    3 buttercup    2    1    1
    V8      6   2   2   1   1   1   1   1   1   3    3    3    2    2    3 orchid    3 buttercup    3    1    1
    V9      7   2   2   1   1   1   1   1   1   3    3    3    2    2    3 orchid    3 buttercup    3    1    1
    V10     8   2   2   4   4   3   3   4   4   2    2    2    3    2    3           3     lilac    3    2    2
    

    I do notice that there are both a 999 value that is probably a missing value indicator, as well as two different values for missing in the factor columns. That is a side-effect of how read.table input the columns. It “thought” that the V3 and V4 columns were numeric and handled sequential commas as a true missing, whereas all the other columns (before transposition) were seen as factor or character variables and sequential commas got turned into “” which is not the same as _NA_character or the NA for factors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear stackoverflow members, I have a small problem, I want to replace some characters
Dear stackoverflow community, I have a simple menu that have to be dynamics. With
Dear members of the Stackoverflow community, We are developing a web application using the
Dear Friends from Stackoverflow, Please help me with a problem that i'm having when
Dear SQL Gurus from Stack Overflow: Environment: Oracle I'm trying to understand why I
dear all..i have a table it looks like: Name version DDX 01 DTX 05
Dear gods of Stackoverflow Let's say I have a MySQL query that selects a
Dear g++ hackers, I have the following question. When some data of an object
Dear Stack Overflow Community, I have a question regarding how to incorporate a WHERE
Dear Stack Overflow Community, I'm attempting to get the date from a datepicker and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.