R - Nested list to (wide) dataframe - r
I currently have the following problem: I extracted some data via the crunchbase API, resulting in a big nested list of the following structure (there are many more nested lists on several instances included, I here only display the part of the structure currently relevant for me):
> str(x[[1]])
$ uuid : chr "5f9957b0841251e6e439d757XXXXXX"
$ relationships: List of 27
..$ websites: List of 3
.. ..$ cardinality: chr "OneToMany"
.. ..$ items :'data.frame': 4 obs. of 7 variables:
.. .. ..$ properties.website_type: chr [1:4] "homepage" "facebook" "twitter" "linkedin"
.. .. ..$ properties.url : chr [1:4] "http://www.example.com" "https://www.facebook.com/example" "http://twitter.com/example" "http://www.linkedin.com/company/example"
Consider the following minimal example:
x <- list()
x[[1]] <- list(uuid = "123",
relationships = list(websites = list(items = list(
properties.website_type = c("homepage", "facebook", "twitter", "linkedin"),
properties.url = c("www.example1.com", "www.fbex1.com", "www.twitterex1.com", "www.linkedinex1.com") ) ) ) )
x[[2]] <- list(uuid = "987",
relationships = list(websites = list(items = list(
properties.website_type = c("homepage", "facebook", "twitter" ),
properties.url = c("www.example2.com", "www.fbex2.com", "www.twitterex2.com") ) ) ) )
Now, I would like to create a dataframe with the following column structure:
> x.df
uuid web.url web.facebook web.twitter web.linkedin
1 123 www.example1.com www.fbex1.com www.twitterex1.com www.linkedinex1.com
2 987 www.example2.com www.fbex2.com www.twitterex2.com <NA>
Meaning: I would like to have every uuid (a unique firm identifier) in a single column, followed by the urls of the different platforms (fb, twitter...). I tried a lot of different things with a combination of lapply(), spread(), and row_bind(), yet didn't manage to make anything work. Any help on that would be appreciated.
dplyr approach could be
library(dplyr)
library(tidyr)
#convert list to dataframe in long format
df <- do.call(rbind, lapply(x, data.frame, stringsAsFactors = FALSE))
#final result
df1 <- df %>%
spread(relationships.websites.items.properties.website_type, relationships.websites.items.properties.url)
which gives
uuid facebook homepage linkedin twitter
1 123 www.fbex1.com www.example1.com www.linkedinex1.com www.twitterex1.com
2 987 www.fbex2.com www.example2.com <NA> www.twitterex2.com
Sample data:
x <- list(structure(list(uuid = "123", relationships = structure(list(
websites = structure(list(items = structure(list(properties.website_type = c("homepage",
"facebook", "twitter", "linkedin"), properties.url = c("www.example1.com",
"www.fbex1.com", "www.twitterex1.com", "www.linkedinex1.com"
)), .Names = c("properties.website_type", "properties.url"
))), .Names = "items")), .Names = "websites")), .Names = c("uuid",
"relationships")), structure(list(uuid = "987", relationships = structure(list(
websites = structure(list(items = structure(list(properties.website_type = c("homepage",
"facebook", "twitter"), properties.url = c("www.example2.com",
"www.fbex2.com", "www.twitterex2.com")), .Names = c("properties.website_type",
"properties.url"))), .Names = "items")), .Names = "websites")), .Names = c("uuid",
"relationships")))
Update: In order to fix below error
Error in (function (..., row.names = NULL, check.rows = FALSE,
check.names = TRUE, : arguments imply differing number of rows: 1,
0
you would need to remove corrupted elements from input data where website_type has one value but properties.url has NULL. Run this chunk of code as a pre-processing step before executing the main solution:
idx <- which(sapply(x, function(k) is.null(k$relationships$websites$items$properties.url)))
x <- x[-idx]
Sample data to test this pre-processing step:
x <- list(structure(list(uuid = "123", relationships = structure(list(
websites = structure(list(items = structure(list(properties.website_type = c("homepage",
"facebook", "twitter", "linkedin"), properties.url = c("www.example1.com",
"www.fbex1.com", "www.twitterex1.com", "www.linkedinex1.com"
)), .Names = c("properties.website_type", "properties.url"
))), .Names = "items")), .Names = "websites")), .Names = c("uuid",
"relationships")), structure(list(uuid = "987", relationships = structure(list(
websites = structure(list(items = structure(list(properties.website_type = "homepage",
properties.url = NULL), .Names = c("properties.website_type",
"properties.url"))), .Names = "items")), .Names = "websites")), .Names = c("uuid",
"relationships")), structure(list(uuid = "345", relationships = structure(list(
websites = structure(list(items = structure(list(properties.website_type = "homepage",
properties.url = NULL), .Names = c("properties.website_type",
"properties.url"))), .Names = "items")), .Names = "websites")), .Names = c("uuid",
"relationships")))
I know this is a clunkier solution, but it helped me seeing the process step by step (running str (x_df) to see each result):
library(tidyverse)
# Using your example
x <- list()
x[[1]] <- list(uuid = "123",
relationships = list(websites = list(items = list(
properties.website_type = c("homepage", "facebook", "twitter", "linkedin"),
properties.url = c("www.example1.com", "www.fbex1.com", "www.twitterex1.com", "www.linkedinex1.com") ) ) ) )
x[[2]] <- list(uuid = "987",
relationships = list(websites = list(items = list(
properties.website_type = c("homepage", "facebook", "twitter" ),
properties.url = c("www.example2.com", "www.fbex2.com", "www.twitterex2.com") ) ) ) )
# --- Iterations of unnest:
x_df <- x %>% tibble::as_tibble_col( .) %>%
tidyr::unnest_wider(col = "value") %>%
tidyr::unnest_longer(col = "relationships") %>%
tidyr::unnest_wider(col = "relationships") %>%
tidyr::unnest_wider(col = "items") %>%
tidyr::unnest_longer(col = c("properties.website_type", "properties.url")) %>%
# --- Lastly, group by id:
group_by(uuid) %>%
tidyr::pivot_wider(data = .,
names_from = properties.website_type,
values_from = c("properties.url"))
Related
Lexis function not found in R
I am using this code from the R help guide in the Epi package: # A small bogus cohort xcoh <- structure( list( id = c("A", "B", "C"), birth = c("14/07/1952", "01/04/1954", "10/06/1987"), entry = c("04/08/1965", "08/09/1972", "23/12/1991"), exit = c("27/06/1997", "23/05/1995", "24/07/1998"), fail = c(1, 0, 1) ), .Names = c("id", "birth", "entry", "exit", "fail"), row.names = c("1", "2", "3"), class = "data.frame" ) # Define a Lexis object with timescales calendar time and age Lcoh <- Lexis( entry = list( per=entry ), exit = list( per=exit, age=exit-birth ), exit.status = fail, data = xcoh ) But I get this error: Error in Lexis(entry = list(per = entry), exit = list(per = exit, age = exit - : could not find function "Lexis" Any thoughts?
Epi package first needs to be installed in the environment using: install.packages("Epi") And then the library for Epi needs to be loaded. library(Epi) Hence your code being modified as follows: install.packages("Epi") library(Epi) xcoh <- structure( list( id = c("A", "B", "C"), birth = c("14/07/1952", "01/04/1954", "10/06/1987"), entry = c("04/08/1965", "08/09/1972", "23/12/1991"), exit = c("27/06/1997", "23/05/1995", "24/07/1998"), fail = c(1, 0, 1) ), .Names = c("id", "birth", "entry", "exit", "fail"), row.names = c("1", "2", "3"), class = "data.frame" ) # Define a Lexis object with timescales calendar time and Lcoh <- Lexis( entry = list( per=entry ), exit = list( per=exit, age=exit-birth ), exit.status = fail, data = xcoh ) Note: I have removed the line that says age. Assuming it is not relevant to the question posted here.
How to create new column from existing column value in R dynamically
i have data frame called df,how to create new column from existing list column data frame. my data frame. Policy Item Checked list(Processed = "Valid", Gmail = "yy#gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY"))) Sample list(Processed = "Valid", Gmail = "tt#gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT"))) Processed list(Processed = "Valid", Gmail = "pp#gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU"))) my expected data frame. Policy Processed Gmail Descrption VID Checked Valid yy#gmail "T1,R1" "YUY" Sample Valid tt#gmail "D3,Y3" "RT" Processed Valid pp#gmail "Y2,LE" "UIU" i'm using below code to get my expected dataframe . na_if_null <- function(x) if (is.null(x)) NA else x new_cols <- lapply( Filter(is.list, df), function(list_col) { names_ <- setNames(nm = unique(do.call(c, lapply(list_col, names)))) lapply(names_, function(name) sapply(list_col, function(x) trimws(na_if_null(as.list(x)[[name]])))) } ) res <- do.call( data.frame, c( list(df, check.names = FALSE, stringsAsFactors = FALSE), do.call(c, new_cols) ) ) But i'm getting below Data frame.please help me to done my post. Policy Item Item.Processed Item.Gmail Item.Information Checked list(Processed = "Valid", Gmail = "yy#gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY"))) Processed yy#gmail list(Descrption = "T1, R1", VID = "YUY") Sample list(Processed = "Valid", Gmail = "tt#gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT"))) Processed tt#gmail list(Descrption = "D3, Y3", VID = "RT") Processed list(Processed = "Valid", Gmail = "pp#gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU"))) Processed pp#gmail list(Descrption = "Y2, LE", VID = "UIU") dput structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list( structure(list(Processed = "Valid", Gmail = "yy#gmail", Information = list( structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption", "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed", "Gmail", "Information"), class = "data.frame", row.names = 1L), structure(list(Processed = "Valid", Gmail = "tt#gmail", Information = list( structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption", "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed", "Gmail", "Information"), class = "data.frame", row.names = 1L), structure(list(Processed = "Valid", Gmail = "pp#gmail", Information = list( structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption", "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed", "Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA, 3L), class = "data.frame", .Names = c("Policy", "Item")) Sample data frame Policy colval Item Checked list(PID="4",Bdetail ="ui,89") list(Processed = "Valid", Gmail = "yy#gmail", Information = list(list(Descrption = "T1, R1", VID = "YUY"))) Sample list(PID="7",Bdetail ="ju,78") list(Processed = "Valid", Gmail = "tt#gmail", Information = list(list(Descrption = "D3, Y3", VID = "RT"))) Processed list(PID ="8",Bdetail ="nj,45") list(Processed = "Valid", Gmail = "pp#gmail", Information = list(list(Descrption = "Y2, LE", VID = "UIU")))
Here a solution in base R: dd <- cbind( dx$Policy, do.call(rbind, lapply(seq_len(nrow(dx)), function(i)unlist(dx$Item[i])) ) ) colnames(dd) <- c("Policy","Processed","Gmail","Descrption","VID") dd # Policy Processed Gmail Descrption VID # [1,] "Checked" "Valid" "yy#gmail" "T1, R1" "YUY" # [2,] "Sample" "Valid" "tt#gmail" "D3, Y3" "RT" # [3,] "Processed" "Valid" "pp#gmail" "Y2, LE" "UIU" Basically I am using unlist for each item. and Then joining them using the classic d.call(rbind,llist). edit in case you want tu use the same names as the original sub lists you can do something like : colnames(dd) <- c("Policy",gsub(".*[.]","",colnames(dd)[-1])) data.table solution library(data.table) setDT(dx) dx[, rbindlist(lapply(.SD,function(x)data.table(t(unlist(x))))),Policy]
Easily done with unnest from tidyr: library(dplyr) library(tidyr) df %>% unnest() %>% unnest() Result: Policy Processed Gmail Descrption VID 1 Checked Valid yy#gmail T1, R1 YUY 2 Sample Valid tt#gmail D3, Y3 RT 3 Processed Valid pp#gmail Y2, LE UIU Data: df = structure(list(Policy = c("Checked", "Sample", "Processed"), Item = list( structure(list(Processed = "Valid", Gmail = "yy#gmail", Information = list( structure(list(Descrption = "T1, R1", VID = "YUY"), .Names = c("Descrption", "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed", "Gmail", "Information"), class = "data.frame", row.names = 1L), structure(list(Processed = "Valid", Gmail = "tt#gmail", Information = list( structure(list(Descrption = "D3, Y3", VID = "RT"), .Names = c("Descrption", "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed", "Gmail", "Information"), class = "data.frame", row.names = 1L), structure(list(Processed = "Valid", Gmail = "pp#gmail", Information = list( structure(list(Descrption = "Y2, LE", VID = "UIU"), .Names = c("Descrption", "VID"), class = "data.frame", row.names = 1L))), .Names = c("Processed", "Gmail", "Information"), class = "data.frame", row.names = 1L))), row.names = c(NA, 3L), class = "data.frame", .Names = c("Policy", "Item")) Note: Notice I used two passes of unnest because there are two levels of lists in your original dataframe. unnest automatically flattens all lists in the dataframe and reuses the names, but it does not do it recursively, so you will have to have as many unnest as there are list levels.
Delete elements with specific field from list (r language)
I have a vector of lists in it. Every list contain field category I need delete all lists with fields category == resource. I tried for(i in 1:length(myList)){ if(myList[[as.numeric(i)]]$data$category == "resource"){ myList[[as.numeric(i)]]<-NULL } } Is it correct? > dput(myList[1:2]) list(structure(list(data = structure(list(device = "iPad2,4", os_version = "ios 7.1.2", connection_type = "wifi", category = "user", platform = "ios", session_num = 2, ios_idfv = "E12AA218-4061-4BA1-AFA3-47FEE1511C2E", client_ts = 1454926346, sdk_version = "unity 2.4.3", limited_ad_tracking = TRUE, user_id = "436E8588-B2FA-411A-896C-5757E7A2A377", manufacturer = "apple", jailbroken = TRUE, ios_idfa = "436E8588-B2FA-411A-896C-5757E7A2A377", build = "1.0", session_id = "558910f2-7c2c-4280-84fc-2fef1a50d291", v = 2, engine_version = "unity 4.6.9"), .Names = c("device", "os_version", "connection_type", "category", "platform", "session_num", "ios_idfv", "client_ts", "sdk_version", "limited_ad_tracking", "user_id", "manufacturer", "jailbroken", "ios_idfa", "build", "session_id", "v", "engine_version")), first_in_batch = TRUE, country_code = "RO", arrival_ts = 1454926344, game_id = 24540, ip = "93.168.249.0"), .Names = c("data", "first_in_batch", "country_code", "arrival_ts", "game_id", "ip")), structure(list( data = structure(list(os_version = "ios 9.2.1", engine_version = "unity 4.6.9", category = "user", v = 2, ios_idfa = "F06962FE-FCE5-475F-8CC2-83FF1F89E573", sdk_version = "unity 2.4.3", user_id = "F06962FE-FCE5-475F-8CC2-83FF1F89E573", session_num = 2, platform = "ios", connection_type = "wifi", manufacturer = "apple", client_ts = 1454925528, limited_ad_tracking = TRUE, session_id = "3ab1fbbb-5103-4ad8-bf56-94f70fea94a7", device = "iPad4,1", ios_idfv = "2C940E82-B074-4A15-B9A7-A5983759042F", build = "1.0"), .Names = c("os_version", "engine_version", "category", "v", "ios_idfa", "sdk_version", "user_id", "session_num", "platform", "connection_type", "manufacturer", "client_ts", "limited_ad_tracking", "session_id", "device", "ios_idfv", "build")), first_in_batch = TRUE, country_code = "AU", arrival_ts = 1454925528, game_id = 24540, ip = "110.175.52.0"), .Names = c("data", "first_in_batch", "country_code", "arrival_ts", "game_id", "ip" )))
You could use rlist package : library(rlist) filtered_list <- list.exclude(myList, data$category == "ressource")
We could use Filter from base R Filter(function(x) x$data$category!="resource", myList)
Error in two almost identical R functions- one gets executed but not the other
I have written 2 R functions, both almost identical. extract_verb<-function(list1){ sd<-names(list1) ds<-which(sd %in% "VP") if(length(ds)==0){ return(NULL) } else{ js<-c() for(i in 1:length(ds)){ js<-c(js,list1[ds[i]]$VP$VB,list1[ds[i]]$VP$VBD,list1[ds[i]]$VP$VBG, list1[ds[i]]$VP$VBN,list1[ds[i]]$VP$VBP,list1[ds[i]]$VP$VBZ) } return(js) } } and extract_subject<-function(list1){ sd<-names(list1) ds<-which(sd %in% "NP") if(length(ds)==0){ return(NULL)} else{ js<-c() for(i in 1:length(ds)){ js<-c(js,list1[ds[i]]$NP$NN,list1[ds[i]]$NP$NNP,list1[ds[i]]$NP$NNPS, list1[ds[i]]$NP$NNS) } return(js) } } I am applying these functions to some nested lists. When I apply these functions to the same list, the extract_verb function works just fine- but the extract_subject function gives an error as follows: Error in list1[ds[i]]$NP$NN : $ operator is invalid for atomic vectors What might be the reason? The nested list s5 to which I am applying the functions is: s5. $NP [1] "" $NP $NP$JJ [1] "Invoice" $NP$NN [1] "number" $CC [1] "and" [[4]] [[4]]$NP [1] NA [[4]]$NP [[4]]$NP$DT [1] "the" [[4]]$NP$NN [1] "number" [[4]][[3]] [[4]][[3]]$VP [[4]][[3]]$VP$VBN [1] "mentioned" [[4]][[3]][[2]] [[4]][[3]][[2]]$PP [[4]][[3]][[2]]$PP$IN [1] "in" [[4]][[3]][[2]][[2]] [[4]][[3]][[2]][[2]]$NP [1] "" [[4]][[3]][[2]][[2]]$NP [[4]][[3]][[2]][[2]]$NP$DT [1] "the" [[4]][[3]][[2]][[2]]$NP$NN [1] "Bill" [[4]][[3]][[2]][[2]][[3]] [[4]][[3]][[2]][[2]][[3]]$PP [[4]][[3]][[2]][[2]][[3]]$PP$IN [1] "of" [[4]][[3]][[2]][[2]][[3]][[2]] [[4]][[3]][[2]][[2]][[3]][[2]]$NP [[4]][[3]][[2]][[2]][[3]][[2]]$NP$NNP [1] "Lading" extract_verb returned NULL as expected extract_verb(s5) NULL but extract_subject gives an error. extract_subject(s5) Error in list1[ds[i]]$NP$NN : $ operator is invalid for atomic vectors dput output for s5 is as follows. dput(s5) structure(list(NP = "", NP = structure(list(JJ = "Invoice", NN = "number"), .Names = c("JJ", "NN")), CC = "and", structure(list(NP = NA_real_, NP = structure(list( DT = "the", NN = "number"), .Names = c("DT", "NN")), structure(list( VP = structure(list(VBN = "mentioned"), .Names = "VBN"), structure(list(PP = structure(list(IN = "in"), .Names = "IN"), structure(list(NP = "", NP = structure(list(DT = "the", NN = "Bill"), .Names = c("DT", "NN")), structure(list( PP = structure(list(IN = "of"), .Names = "IN"), structure(list( NP = structure(list(NNP = "Lading"), .Names = "NNP")), .Names = "NP")), .Names = c("PP", ""))), .Names = c("NP", "NP", ""))), .Names = c("PP", ""))), .Names = c("VP", ""))), .Names = c("NP", "NP", ""))), .Names = c("NP", "NP", "CC", ""))
Error in View : undefined columns selected
Below is a subsample of my data set (only 2 rows by 215 columns). I am trying to view them on RStudio but it gives me the following error: Error in View : undefined columns selected Do not really know what is going on. The whole set is 7786 rows by 215 columns. Viewing it works fine, however, when doing any kind of subsetting or removing one row it is no longer want to view. structure(list(`NA` = structure(c(16343, 16344), class = "Date"), AVON = c("615.5", "621.5"), BA. = c("471.5", "463.2"), CMRG = c("224.5", "224.5"), COB = c("291.10000000000002", "283.5"), MGGT = c("451.2", "444.7"), QQ. = c("224.5", "223.5"), RR. = c("953.65", "933.38" ), SNR = c("268.2", "264.7"), ULE = c("1771", "1746"), GKN = c("319.2", "311.5"), BRAG = c("617", "603"), BVIC = c("668", "661"), CCH = c("1333", "1327"), DGE = c("1785", "1760.5"), SAB = c("3428", "3383"), STCK = c("291.60000000000002", "294"), ALNT = c("328", "321"), CAR = c("125", "124.5"), CRDA = c("2053", "1990"), ELM = c("255.5", "254.5"), JMAT = c("2919", "2825"), SYNT = c("212", "210.8"), VCTA = c("1606", "1605"), DIA = c("901", "924"), DNO = c("611", "611"), E2V = c("161", "160.5"), HLMA = c("612", "598.5"), HTY = c("309.8", "308"), MGAM = c("296.8", "289.40000000000003" ), OXFD = c("1020", "1035"), RSHW = c("1630", "1625"), SXS = c("1808", "1778"), TTG = c("166.75", "167.5"), XAR = c("376", "367" ), X = c("1527", "1520"), ABF = c("2679", "2654"), AE = c("633.5", "640"), CARM = c("1647", "1637"), CWK = c("1328", "1320"), DCG = c("383.75", "369"), DVO = c("237.75", "231"), GNCL = c("234", "229.6"), HFG = c("416", "411"), FD = c("36.5", "34.75"), TATE = c("591.5", "585"), MNDI = c("1011", "1012"), BI = c("616", "620"), REX = c("491.8", "483.5"), RC = c("559", "540"), SMDS = c("266.3", "257"), SMIN = c("1264", "1250"), VSVS = c("451.8", "438.40000000000003"), AGA = c("163.25", "160.25"), BDEV = c("396.1", "389.3"), BKG = c("2250", "2224"), BLWY = c("1567", "1558" ), BVS = c("779", "771"), CRST = c("325", "314.60000000000002" ), GLSN = c("393.5", "388.5"), MCB = c("83.53", "83.29"), SN = c("1334", "1309"), RB. = c("5350", "5305"), RDW = c("280.7", "273.8"), TW. = c("112.8", "111.8"), BODY = c("668.5", "647" ), FENR = c("317.60000000000002", "313.10000000000002"), GDWN = c("3500", "3500"), HILS = c("561", "561.5"), IMI = c("1230", "1206"), MRO = c("247.70000000000002", "246"), VAR = c("304", "300.75"), RNO = c("56", "54.5"), RTRK = c("2765", "2736" ), SFR = c("63.5", "64"), SRX = c("2826", "2812"), TRI = c("105.75", "105"), VTC = c("613.5", "612"), WEIR = c("2502", "2430"), EVR = c("130", "123.60000000000001"), FXO = c("112.3", "105.10000000000001" ), BBA = c("325", "326"), BMS = c("494.38", "492"), CKN = c("2350", "2341"), FSHR = c("1326", "1294"), RMG = c("392.2", "399.7" ), STOB = c("111", "109"), UKM = c("473.88", "467"), WIN = c("136.25", "137.5"), GAW = c("597.5", "585"), HTM = c("131.5", "129.25" ), `NA` = c(NA_character_, NA_character_), AAL = c("1384", "1363.5"), ABG = c("218.8", "209.1"), ANTO = c("721", "702" ), AF = c("131.5", "130.25"), AQ = c("18.5", "18.75"), ARMS = c("69", "62.25"), BLT = c("1715", "1690.5"), CEY = c("61.15", "61" ), FRES = c("760", "747"), GEMD = c("192", "191.75"), GLEN = c("343.2", "336.45"), HOC = c("135.30000000000001", "130.19999999999999" ), KAZ = c("263.39999999999998", "260.10000000000002"), KMRL = c("9.5", "9.3000000000000007"), LMI = c("185.8", "176.8"), NWR = c("1.97", "1.82"), `NA` = c(NA_character_, NA_character_), DL = c("190.20000000000002", "190"), OG = c("22", "24"), OLY = c("516", "496.6"), RIO = c("3031.5", "3020"), RRS = c("4209", "4154"), VED = c("998.5", "974.5" ), AFR = c("103.5", "109.4"), BG. = c("1140", "1093"), B. = c("453.45", "452.75"), CNE = c("176.5", "171.6"), ENQ = c("109.60000000000001", "107.8"), EXI = c("157", "150"), HDY = c("102", "99.75"), JKX = c("48.25", "47"), OHR = c("229.3", "220.9"), MO = c("333", "324.7"), RDSA = c("2358.5", "2331"), RDSB = c("2437", "2418.5" ), SIA = c("381", "377.90000000000003"), SMDR = c("100", "98.5"), TLW = c("644.5", "631"), AMEC = c("1104", "1077" ), CIU = c("283.5", "275.75"), GMS = c("157", "157"), HTG = c("892.5", "876"), LAM = c("163.25", "160"), FC = c("1037", "1011"), WG. = c("759.5", "743"), BRBY = c("1511", "1476"), ZC = c("365.7", "366"), SG = c("1133", "1126"), TED = c("1863", "1862"), ULVR = c("2585", "2547"), AZN = c("4441.5", "4360.5"), BTG = c("700", "697.5"), CIR = c("304", "300"), DH = c("758", "753"), GNS = c("1130", "1130"), GSK = c("1413", "1414"), HIK = c("1733", "1715"), SH = c("5340", "5310"), SK = c("329.25", "319"), VEC = c("132", "132"), AGK = c("1548", "1528"), AHT = c("1043", "1024"), ATK = c("1317", "1323"), BAB = c("1092", "1085"), BNZL = c("1610", "1597"), BRAM = c("376", "374"), BRSN = c("980", "979"), CLLN = c("304.60000000000002", "304.3"), CMS = c("59.75", "59.5"), CNCT = c("149.25", "151"), CI = c("1164", "1165" ), CTR = c("259.5", "255"), DCC = c("3422", "3405"), DLAR = c("477", "478"), DLM = c("689.5", "685"), ECOM = c("223", "219.8"), ESNT = c("797.5", "792.5"), EXO = c("176.5", "180"), EXN = c("983.5", "968"), GFS = c("250.70000000000002", "251.6"), GFTU = c("626", "616"), HAS = c("116.3", "115.7"), HRG = c("45.75", "45.75" ), HSV = c("319.7", "319"), HWDN = c("339.1", "335"), HYC = c("749", "748"), IRV = c("599.5", "592.5"), ITRK = c("2621", "2631" ), LVD = c("201.75", "201.5"), MER = c("435", "436.75"), MMC = c("25.25", "25"), MNZS = c("569", "575.5"), MI = c("418.6", "421"), MTO = c("287.90000000000003", "286.60000000000002" ), NTG = c("483.8", "481.3"), AY = c("983.5", "989"), FL = c("182", "180.1"), RCDO = c("671", "667.5"), RENT = c("117.8", "116" ), RGU = c("169.70000000000002", "169.9"), RS = c("261", "251.6"), RWA = c("302.5", "302.5"), SDY = c("70.5", "69.75" ), SERC = c("286.10000000000002", "279.8"), SHI = c("166.6", "161.1"), SIV = c("199.75", "200"), SKS = c("90", "92"), STHR = c("350.25", "358.5"), TK = c("1664", "1635"), TRB = c("170.5", "172"), V. = c("609.5", "600"), WOS = c("3242", "3243"), XCH = c("188", "184.75"), ARM = c("906", "887.5"), BVC = c("16.38", "16.25"), CSR = c("758", "756"), IMG = c("188.5", "184.75" ), LRD = c("309.7", "306.7"), IC = c("298.10000000000002", "299"), SEU = c("141", "141"), ST = c("104.60000000000001", "99.9"), BATS = c("3482", "3480"), IMT = c("2664", "2679" )), .Names = c("NA", "AVON", "BA.", "CMRG", "COB", "MGGT", "QQ.", "RR.", "SNR", "ULE", "GKN", "BRAG", "BVIC", "CCH", "DGE", "SAB", "STCK", "ALNT", "CAR", "CRDA", "ELM", "JMAT", "SYNT", "VCTA", "DIA", "DNO", "E2V", "HLMA", "HTY", "MGAM", "OXFD", "RSHW", "SXS", "TTG", "XAR", "X", "ABF", "AE", "CARM", "CWK", "DCG", "DVO", "GNCL", "HFG", "FD", "TATE", "MNDI", "BI", "REX", "RC", "SMDS", "SMIN", "VSVS", "AGA", "BDEV", "BKG", "BLWY", "BVS", "CRST", "GLSN", "MCB", "SN", "RB.", "RDW", "TW.", "BODY", "FENR", "GDWN", "HILS", "IMI", "MRO", "VAR", "RNO", "RTRK", "SFR", "SRX", "TRI", "VTC", "WEIR", "EVR", "FXO", "BBA", "BMS", "CKN", "FSHR", "RMG", "STOB", "UKM", "WIN", "GAW", "HTM", NA, "AAL", "ABG", "ANTO", "AF", "AQ", "ARMS", "BLT", "CEY", "FRES", "GEMD", "GLEN", "HOC", "KAZ", "KMRL", "LMI", "NWR", NA, "DL", "OG", "OLY", "RIO", "RRS", "VED", "AFR", "BG.", "B.", "CNE", "ENQ", "EXI", "HDY", "JKX", "OHR", "MO", "RDSA", "RDSB", "SIA", "SMDR", "TLW", "AMEC", "CIU", "GMS", "HTG", "LAM", "FC", "WG.", "BRBY", "ZC", "SG", "TED", "ULVR", "AZN", "BTG", "CIR", "DH", "GNS", "GSK", "HIK", "SH", "SK", "VEC", "AGK", "AHT", "ATK", "BAB", "BNZL", "BRAM", "BRSN", "CLLN", "CMS", "CNCT", "CI", "CTR", "DCC", "DLAR", "DLM", "ECOM", "ESNT", "EXO", "EXN", "GFS", "GFTU", "HAS", "HRG", "HSV", "HWDN", "HYC", "IRV", "ITRK", "LVD", "MER", "MMC", "MNZS", "MI", "MTO", "NTG", "AY", "FL", "RCDO", "RENT", "RGU", "RS", "RWA", "SDY", "SERC", "SHI", "SIV", "SKS", "STHR", "TK", "TRB", "V.", "WOS", "XCH", "ARM", "BVC", "CSR", "IMG", "LRD", "IC", "SEU", "ST", "BATS", "IMT"), row.names = 7785:7786, class = "data.frame") I am on Mac OS 10.10, R 3.1.1 and RStudio 0.98.1060.
One of your column names is NA. If d is your data defined above, then try names(d)[92]. Try replacing with a non-missing column name.
As allready mentioned by DMC, but with a short version of your example code. a <- structure(list(`NA` = structure(c(16343, 16344), class = "Date"), AVON = c("615.5", "621.5"), BA. = c("471.5", "463.2"), `NA` = c(NA_character_, NA_character_), AAL = c("1384", "1363.5")), .Names = c(NA, "AVON", "BA.", "NA", "AAL"), row.names = 7785:7786, class = "data.frame") View(a) Error in View : undefined columns selected names(a) [1] NA "AVON" "BA." "NA" "AAL" a <- structure(list(`NA` = structure(c(16343, 16344), class = "Date"), AVON = c("615.5", "621.5"), BA. = c("471.5", "463.2"), `NA` = c(NA_character_, NA_character_), AAL = c("1384", "1363.5")), .Names = c("NA", "AVON", "BA.", "NA", "AAL"), row.names = 7785:7786, class = "data.frame") View(a) names(a) [1] "NA" "AVON" "BA." "NA" "AAL" You need to have proper names in the data frame to View it.