Как создать несколько подмножеств кадра данных, передав две переменные - PullRequest
0 голосов
/ 03 июня 2019

Я пытаюсь разбить фрейм данных на несколько фреймов данных на основе нескольких свойств: a.Укажите имена столбцов, которые содержат знак пунктуации, т. Е. (1), (2), (3) и т. Д. B.Разделите фрейм данных между двумя местоположениями столбцов, указанными для .eg

  • Первое идентифицированное местоположение = 2
  • Второе идентифицированное местоположение = 11

    Ожидаемый вывод = NewDataSet [, 2:10] и т. Д.

Это форма данных, которую мы получаем ежемесячно, и нам необходимо обработать ее, чтобы сделать ее совместимой с базой данных SQL

structure(list(`Document ID` = c(159812L, 159822L, 170083L), 
`Rental unit (1)` = 1:3, `Gross area` = structure(1:3, .Label = c("1,000.00", 
"1,001.00", "1,002.00"), class = "factor"), Unit = structure(c(1L, 
1L, 1L), .Label = "sq ft", class = "factor"), `Net area` = structure(c(1L, 
1L, 1L), .Label = "n/a", class = "factor"), Unit = c(NA, 
NA, NA), `Floor no.` = structure(c(1L, 1L, 1L), .Label = "n/a", class = "factor"), 
Unit = c(NA, NA, NA), `Start date` = structure(1:3, .Label = c("6/3/2008", 
"7/20/2007", "n/a"), class = "factor"), `End date` = structure(c(2L, 
1L, 3L), .Label = c("6/29/2025", "6/30/2028", "n/a"), class = "factor"), 
`Rental unit (2)` = 3:5, `Gross area` = structure(1:3, .Label = c("1,000.00", 
"1,001.00", "1,002.00"), class = "factor"), Unit = structure(c(1L, 
1L, 1L), .Label = "sq ft", class = "factor"), `Net area` = structure(c(1L, 
1L, 1L), .Label = "n/a", class = "factor"), Unit = c(NA, 
NA, NA), `Floor no.` = structure(c(1L, 1L, 1L), .Label = "n/a", class = "factor"), 
Unit = c(NA, NA, NA), `Start date` = structure(1:3, .Label = c("6/3/2008", 
"7/20/2007", "n/a"), class = "factor"), `End date` = structure(c(2L, 
1L, 3L), .Label = c("6/29/2025", "6/30/2028", "n/a"), class = "factor"), 
`Rental unit (3)` = 5:7, `Gross area` = structure(1:3, .Label = c("1,000.00", 
"1,001.00", "1,002.00"), class = "factor"), Unit = structure(c(1L, 
1L, 1L), .Label = "sq ft", class = "factor"), `Net area` = structure(c(1L, 
1L, 1L), .Label = "n/a", class = "factor"), Unit = c(NA, 
NA, NA), `Floor no.` = structure(c(1L, 1L, 1L), .Label = "n/a", class = "factor"), 
Unit = c(NA, NA, NA), `Start date` = structure(1:3, .Label = c("6/3/2008", 
"7/20/2007", "n/a"), class = "factor"), `End date` = structure(c(2L, 
1L, 3L), .Label = c("6/29/2025", "6/30/2028", "n/a"), class = "factor"), 
Longitude = c(NA, NA, NA), Latitude = c(NA, NA, NA), `Orga Unit` = structure(c(2L, 
2L, 1L), .Label = c("SESAC and Sublease", " 2018 - Real Estate Lease Demo"
), class = "factor"), `Workflow state` = structure(c(1L, 
1L, 1L), .Label = "R1 + R2 done", class = "factor"), `Name of DocSet` = structure(c(3L, 
1L, 2L), .Label = c("ii - 1000 - Target", "SESAC", "Stop & Shop executed lease 060308"
), class = "factor"), `Language of DocSet` = structure(c(2L, 
2L, 1L), .Label = c("en", "en_US"), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

несколько фреймов данных в зависимости от местоположения столбца

Я начал с приведенного ниже кода:

newFile <-  read.csv("sample.csv", check.names = FALSE)
vecLoc <- c(grep("[[:punct:]]", colnames(newFile)))

1 Ответ

1 голос
/ 05 июня 2019
data=structure(list(`Document ID` = c(159812L, 159822L, 170083L), 
`Rental unit (1)` = 1:3, `Gross area` = structure(1:3, .Label = c("1,000.00", 
"1,001.00", "1,002.00"), class = "factor"), Unit = structure(c(1L, 
1L, 1L), .Label = "sq ft", class = "factor"), `Net area` = structure(c(1L, 
1L, 1L), .Label = "n/a", class = "factor"), Unit = c(NA, 
NA, NA), `Floor no.` = structure(c(1L, 1L, 1L), .Label = "n/a", class = "factor"), 
Unit = c(NA, NA, NA), `Start date` = structure(1:3, .Label = c("6/3/2008", 
"7/20/2007", "n/a"), class = "factor"), `End date` = structure(c(2L, 
1L, 3L), .Label = c("6/29/2025", "6/30/2028", "n/a"), class = "factor"), 
`Rental unit (2)` = 3:5, `Gross area` = structure(1:3, .Label = c("1,000.00", 
"1,001.00", "1,002.00"), class = "factor"), Unit = structure(c(1L, 
1L, 1L), .Label = "sq ft", class = "factor"), `Net area` = structure(c(1L, 
1L, 1L), .Label = "n/a", class = "factor"), Unit = c(NA, 
NA, NA), `Floor no.` = structure(c(1L, 1L, 1L), .Label = "n/a", class = "factor"), 
Unit = c(NA, NA, NA), `Start date` = structure(1:3, .Label = c("6/3/2008", 
"7/20/2007", "n/a"), class = "factor"), `End date` = structure(c(2L, 
1L, 3L), .Label = c("6/29/2025", "6/30/2028", "n/a"), class = "factor"), 
`Rental unit (3)` = 5:7, `Gross area` = structure(1:3, .Label = c("1,000.00", 
"1,001.00", "1,002.00"), class = "factor"), Unit = structure(c(1L, 
1L, 1L), .Label = "sq ft", class = "factor"), `Net area` = structure(c(1L, 
1L, 1L), .Label = "n/a", class = "factor"), Unit = c(NA, 
NA, NA), `Floor no.` = structure(c(1L, 1L, 1L), .Label = "n/a", class = "factor"), 
Unit = c(NA, NA, NA), `Start date` = structure(1:3, .Label = c("6/3/2008", 
"7/20/2007", "n/a"), class = "factor"), `End date` = structure(c(2L, 
1L, 3L), .Label = c("6/29/2025", "6/30/2028", "n/a"), class = "factor"), 
Longitude = c(NA, NA, NA), Latitude = c(NA, NA, NA), `Orga Unit` = structure(c(2L, 
2L, 1L), .Label = c("SESAC and Sublease", " 2018 - Real Estate Lease Demo"
), class = "factor"), `Workflow state` = structure(c(1L, 
1L, 1L), .Label = "R1 + R2 done", class = "factor"), `Name of DocSet` = structure(c(3L, 
1L, 2L), .Label = c("ii - 1000 - Target", "SESAC", "Stop & Shop executed lease 060308"
), class = "factor"), `Language of DocSet` = structure(c(2L, 
2L, 1L), .Label = c("en", "en_US"), class = "factor")), class = "data.frame", row.names = c(NA, 
-3L))

Итак, я нашел расположение нужных вам столбцов следующим образом:

split_locations=grep(colnames(data),pattern = "[(*)]") # the strategy that you provided in your Q identified columns with 'Floor no.' in them... 

Так что из выше созданного split_locations у меня есть номера столбцов c(2,11,20) - надеюсь, это правильно.

Это всего 3 сплита, поэтому вы можете просто сделать:

df1=data[,2:10]
df2=data[,11:19]
df3=data[,20:ncol(data)]

Однако, если вышесказанное слишком просто для фактического анализа, который вы делаете. Вы можете сделать следующее:

split_locations=c(split_locations,ncol(data)) #add the final number to the end of split_locations
iterate_to=length(split_locations)-1 #specify how far we'll be iterating
for(i in 1:iterate_to){ #from 1 to the second last element of split_locations
    assign(paste0('df',i), data[,c(split_locations[i]:split_locations[i+1])]) #use the command 'assign' to assign data to 'df1', 'df2' etc.
}

Приведенный выше код присваивает разделенные столбцы разным фреймам данных: столбцы с 2:10 до df1, с 11:19 до df2 и т. Д. Для этого используются номера столбцов от i до i+1. Надеюсь, это имеет смысл.

Если вы хотите, вы также можете записать разделенные столбцы в их собственные текстовые файлы с помощью write.table(data[,c(split_locations[i]:split_locations[i+1])],file=paste0('data',i,'.txt'),....)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...