r - Naming and adding columns to a dataframe -


I am working to add different text files to a data frame,

so far I have

  files = list.files () data = lapply (files, function (x) read.table (x)) data_rbind & lt; - do.call ("rbind", data)   

I would like to add column headings, I have tried

  colnames (data_rbind) = c ( 'Car', 'color', 'price')   

However, this error message returns

error colnames & lt; - The name 'attribute [3] should have the same length of the vector [1] - ( * tmp * , value = ("car", "color", "price"))

I want to add the values ​​associated with the name of each file to add 4th column called 'year', is it possible?

Edit - I have realized that the above error is because my dataframe reads only as 1 variable when it should be 3. 3 columns all begin to be contained within 1 column. I'm not sure why this happened

I want to separate the data into 3 separate columns and to read the structure function

  data.frame '9001 Object 3 variables: $ V1: chr "Toyota" "BMW" "Ford" ... $ V2: chr "Blue" "Red" "Red" "Silver" ... $ V3: int 80412 65443 47 920 41560 38019 29618 29074 28885 2615 9 25 9 3 ...   

I have several basic rows in each file, one of which has 3 values, separated by commas, for example,

Toyota, Silver, 6544

BMW, Red, 4792

explains the error That is, the number of your vector names is not of the same length, as the number of columns in your dataframe. If you want to set specific column names, do the following

  #seume car, color, and price according to column 1, 3, 4 colnames (dataframe) [c (1 , 3, 4)] & lt; -c ("car", "color", "value")   

If you want to add a descriptive year, you can call it

  df $ year & lt; -values ​​  

However you will need to ensure that the number of entries in the values ​​is equal to the number of rows in the DF for the desired result. The name of that column will then be "year"

Just to show that it works, I have created a simple txt file based on the provided example of the question. After being lost, I'll be able to send a message to me.

  # [[1]] # V1V2V3 # Ford Raid 8041 # 2 Toyota Silver 6544 # 3 BMW Rad 4792 # # [[2]] # V1V2V3 # 1 Ford Red 8041 # 2 Toyota Silver 6544 # 3 BMW Red 4792 # # [[3]] # V1V2V # 1 Ford Raid 8041 # 2 Toyota Silver 6544 # 3 BMW Red 4792   

Then after do.call

  # V1V2V3 # 1 Ford Raid 8041 # 2 Toyota Silver 6544 # 3 BMW Red 4792 # 4 Ford Raid 8041 # 5 Toyota Silver 6544 # 6 BMW   

Red 4792 # 7 Ford Red 8041 # 8 Toyota Silver 6544 # 9 BMW Red 4792

Then we can change the column name, clear about indeces indeces

  colnames ( Dataframe) [c (1,2,3)] & lt; -c ("car", "color", "value")   

We can also add the year

  dataframe $ year and lieutenant; -1 # All year 1 # Color of the car Year 1 1 Ford red 8041 1 # 2 Toyota Silver 6544 1 # 3 BMW Red 4792 1 # 4 Ford Raid 8041 1 # 5 Toyota Silver 6544 1 # 6 BMW Red 4792 1 # 7 Ford Raid 8041 1 # 8 Toyota Silver 6544 1 # 9 BMW Red 4792 1 And Here is a Dupu Lap Call  
  List (Structure (list ( V1 = C ("Ford", "Toyota", "BMW"), V2 = C ("red", "name = c (" v1 "," v2 "," v3 "), class = "Data.frame", line.names ========================================= ==================== ============================== "C (NA, -3L), structure (list (V1 = C (" V. 2 = c ("red", "silver", "red"), v3 = c (8041 l, 6544 l, 47 9 l)), named after "Ford", "Toyota", "BMW" = C ("V1", "V2", "V3"), class = "data.frame", line.Name = c (na, -3l)), structure (list (v1 = c ( "Ford", "Toyota", "BMW"), V2 = C ("red", "silver", "red"), v3 = c (8041 l, 6544 l, 47 9 l)). = C (c, "V1", "V2", "V3"), class = "data.frame", line.Name = C (NA, -3L))    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -